GH-160 - Prevent missing ArchUnit runtime dependency.

Adds ArchUnit as an explicit dependency to spring-modulith-runtime. Registers auto-configuration that throws an exception in case ArchUnit is missing from the classpath (as it is likely that it's declared as test scope dependency only) and a corresponding failure analyzer to give guidance on how to solve the problem.
This commit is contained in:
Michael Weirauch
2023-03-06 23:38:22 +01:00
committed by Oliver Drotbohm
parent e16efe92d2
commit 595a052702
6 changed files with 116 additions and 0 deletions

View File

@@ -22,6 +22,12 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit</artifactId>
<version>${archunit.version}</version>
</dependency>
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.modulith.runtime.autoconfigure;
/**
* An Exception carrying information about a missing runtime dependency to be
* analyzed by {@link MissingRuntimeDependencyFailureAnalyzer}.
*
* @author Michael Weirauch
*/
class MissingRuntimeDependencyException extends RuntimeException {
private static final long serialVersionUID = 1L;
private final String dependencyName;
public MissingRuntimeDependencyException(String dependencyName) {
this.dependencyName = dependencyName;
}
public String getDependencyName() {
return dependencyName;
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.modulith.runtime.autoconfigure;
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis;
import org.springframework.boot.diagnostics.FailureAnalyzer;
/**
* {@link FailureAnalyzer} for {@link MissingRuntimeDependencyException}.
*
* @author Michael Weirauch
*/
class MissingRuntimeDependencyFailureAnalyzer extends AbstractFailureAnalyzer<MissingRuntimeDependencyException> {
@Override
protected FailureAnalysis analyze(Throwable rootFailure, MissingRuntimeDependencyException cause) {
return new FailureAnalysis(
String.format("Spring Modulith requires the dependency '%s' to be on the runtime classpath.",
cause.getDependencyName()),
"Add the missing dependency to the runtime classpath of your project.", cause);
}
}

View File

@@ -25,6 +25,7 @@ import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
@@ -45,10 +46,20 @@ import org.springframework.util.Assert;
* Bean.
*
* @author Oliver Drotbohm
* @author Michael Weirauch
*/
@AutoConfiguration
class SpringModulithRuntimeAutoConfiguration {
@AutoConfiguration
@ConditionalOnMissingClass("com.tngtech.archunit.core.importer.ClassFileImporter")
static class ArchUnitRuntimeDependencyMissingConfiguration {
ArchUnitRuntimeDependencyMissingConfiguration() {
throw new MissingRuntimeDependencyException("archunit");
}
}
private static final Logger LOGGER = LoggerFactory.getLogger(SpringModulithRuntimeAutoConfiguration.class);
private final AsyncTaskExecutor executor = new SimpleAsyncTaskExecutor();

View File

@@ -0,0 +1,3 @@
# Failure analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.modulith.runtime.autoconfigure.MissingRuntimeDependencyFailureAnalyzer

View File

@@ -18,16 +18,21 @@ package org.springframework.modulith.runtime.autoconfigure;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanInstantiationException;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
import org.springframework.modulith.runtime.ApplicationRuntime;
import com.tngtech.archunit.core.importer.ClassFileImporter;
/**
* Integration test for {@link SpringModulithRuntimeAutoConfiguration}.
*
* @author Oliver Drotbohm
* @author Michael Weirauch
*/
class SpringModulithRuntimeAutoConfigurationIntegrationTests {
@@ -48,4 +53,22 @@ class SpringModulithRuntimeAutoConfigurationIntegrationTests {
context.close();
});
}
@Test
void missingArchUnitRuntimeDependencyEscalatesOnContextStartup() {
new ApplicationContextRunner()
.withUserConfiguration(SampleApp.class)
.withConfiguration(AutoConfigurations.of(SpringModulithRuntimeAutoConfiguration.class))
.withClassLoader(new FilteredClassLoader(ClassFileImporter.class))
.run(context -> {
assertThat(context).hasFailed();
assertThat(context.getStartupFailure().getCause())
.isInstanceOf(BeanInstantiationException.class)
.cause().isInstanceOf(MissingRuntimeDependencyException.class);
context.close();
});
}
}