我有一个Java项目,正在使用JDK 1.8Maven 3.9构建工具.

以下是前pom.xml名:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mycompany</groupId>
    <artifactId>testprojectcodestyle</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <compilerVersion>${java.version}</compilerVersion>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

现在,当我在项目上执行mvn compile阶段时.我看到它编译了5个文件.那好吧.

现在,我只更改了一个文件,并再次执行了mvn compile阶段.它重新编译了这5个文件.

为什么会发生这种事?可以告诉maven只编译更改过的文件吗?

推荐答案

您需要使用maven-compiler-plugin中的<useIncrementalCompilation>功能.

阅读Maven的useIncrementalCompilation人文档:

只需将以下代码添加到maven-编译器-plugin:

<useIncrementalCompilation>false</useIncrementalCompilation>

这将确保更改后的文件仅用于编译.

更新后的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mycompany</groupId>
    <artifactId>testprojectcodestyle</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.2.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <compilerVersion>${java.version}</compilerVersion>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <useIncrementalCompilation>false</useIncrementalCompilation>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Note:try 使用最新版本的maven插件.虽然<useIncrementalCompilation>功能可以从maven-compiler-plugin版本3.1开始使用.

Java相关问答推荐

是否有一种格式模式,可以在除0之外的数字前面有正负符号?

Listview—在Android Java中正确链接项目时出错

最小拓Flutter 排序的时间复杂度是多少?

scanner 如何在执行hasNextLine一次后重新读取整个文件?

同时运行JUnit测试和Selenium/Cucumber测试时出现问题

Java Stream,需要更新列表对象列表

为什么Java的代码工作(if condition内部的实例)

ApachePOI:不带换行的新行

Jlink选项&-strie-ative-Commands";的作用是什么?

如何正确创建序列图?

安装Java Jar应用程序的Install4j遇到ClassNotFoundException的运行时错误

计算两个浮点数之间的距离是否对称?

由于 list 中的权限错误,Android未生成

Jolt变换JSON数组问题

基于接口的投影、原生查询和枚举

Docker不支持弹性APM服务器

Spring Framework6.1中引入的新RestClient是否有适合于测试的变体,就像RestTemplate和TestRestTemplate一样?

我的代码是线程安全的吗?[Java、CAS、转账]

模拟JUnit未检测到返回字符串的方法的任何声纳覆盖

窗口启动后不久,从java.awt.Graphics disapear创建的矩形