mvn clean或mvn clean package没有检测到test文件

目录

  • 背景
  • 场景
  • 解决办法
  • 扩展
  • 参考

背景

利用mvn clean package打包maven项目,需要执行test case,但是执行的过程中test case被忽略了。

场景

文件目录
mvn clean或mvn clean package没有检测到test文件_第1张图片
poml.xml

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.10</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.restdocs</groupId>
			<artifactId>spring-restdocs-mockmvc</artifactId>
			<version>2.0.4.RELEASE</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
			</plugin>

			<plugin>
				<groupId>org.asciidoctor</groupId>
				<artifactId>asciidoctor-maven-plugin</artifactId>
				<version>1.5.8</version>
				<executions>
					<execution>
						<id>generate-docs</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>process-asciidoc</goal>
						</goals>
						<configuration>
							<backend>html</backend>
							<doctype>book</doctype>
							<attributes>
								<build-time>{$maven.build.timestamp}</build-time>
								<snippetsDirectory>${project.build.directory}/generated-snippets</snippetsDirectory>
							</attributes>
						</configuration>
					</execution>
				</executions>
				<dependencies>
					<dependency>
						<groupId>org.springframework.restdocs</groupId>
						<artifactId>spring-restdocs-asciidoctor</artifactId>
						<version>2.0.4.RELEASE</version>
					</dependency>
				</dependencies>
			</plugin>

			<plugin>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.7</version>
				<executions>
					<execution>
						<id>copy-resources</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>copy-resources</goal>
						</goals>
						<configuration>
							<outputDirectory>
								${project.build.outputDirectory}/static/docs
							</outputDirectory>
							<resources>
								<resource>
									<directory>
										${project.build.directory}/generated-docs
									</directory>
								</resource>
							</resources>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

test文件LoginControllerTest

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class LoginControllerTest {
    @Rule
    public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("target/generated-snippets");

    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext context;

    @Before
    public void setUp() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
                .apply(documentationConfiguration(this.restDocumentation))
                .build();
    }

    @Test
    public void contextLoads() throws Exception {
        this.mockMvc.perform(get("/login"))
                .andExpect(status().isOk())
                 .andDo(document("{ClassName}/{methodName}"))
        ;
    }
}

执行mvn clean package后没有test
mvn clean或mvn clean package没有检测到test文件_第2张图片

解决办法

因为mvn clean package是success的,没有报错,无法知道错误的原因,所以执行mvn -X查看可能导致失败的原因。错误如下:
mvn clean或mvn clean package没有检测到test文件_第3张图片

本文的错误在于No goals have been specified for this build。在poml.xml文件的标签添加compile, 在maven-surefire-plugin插件显示指定版本号。
mvn clean或mvn clean package没有检测到test文件_第4张图片
问题解决了。

扩展

执行mvn test或者mvn clean package语句中没有检测到test文件的其他可能原因及解决办法 可以查看这个文章.,里面有很多种解决办法,不同环境不同的配置有不同的导致原因。这个问题折腾了我一天,希望本文对你有所帮助。

参考

https://blog.csdn.net/qq_33323054/article/details/105115806.

你可能感兴趣的:(maven,java,maven)