仅编译Maven中的选定文件

Mic*_*ski 25 java maven

我想只编译源目录中选定的文件或目录(包括子目录).我敢肯定,我可以做到这一点使用<includes>maven-compiler-plugin的配置,但似乎如我所料,因为它仍然编译所有的类到不行target/classes.真正奇怪的是,Maven输出表明该设置实际上是有效的,因为:

  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.5.1</version>
    <configuration>
      <includes>
        <include>com/example/dao/bean/*.java</include>
      </includes>
    </configuration>
  </plugin>
Run Code Online (Sandbox Code Playgroud)

我有:

[INFO] Compiling 1 source file to c:\Projects\test\target\classes
Run Code Online (Sandbox Code Playgroud)

但是没有编译器的配置,我有:

[INFO] Compiling 14 source file to c:\Projects\test\target\classes
Run Code Online (Sandbox Code Playgroud)

然而,在这两种情况下,所有14个类都编译成target/classes我提到的.你能解释一下或建议另一个解决方案只编译选定的文件吗?

Ily*_*lya 32

简单的应用程序有3个类.

com/company/Obj1.java
com/company/Obj2.java
com/company/inner/Obj3.java  
Run Code Online (Sandbox Code Playgroud)

buildpom.xml

<build>
         <plugins>
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>2.0.2</version>
               <configuration>
                  <source>1.6</source>
                  <target>1.6</target>
                  <includes>
                     <include>com/company/inner/*.java</include>
                  </includes>
               </configuration>
            </plugin>
          </plugins>

   </build>  
Run Code Online (Sandbox Code Playgroud)

结果:编译了1个类.
包含的任何组合都运作良好,
或者你的意思是其他什么?

  • 编译器的2.1版是它最后使用的版本. (7认同)
  • 好.这是我调查的结果.高达2.1的`maven-compiler-plugin`的版本按预期工作 - 它只需要通过其配置的`<includes>`和`<excludes>`标签来编译.版本2.2及更高版本有一些错误(我就是这么看)编译你想要的东西,但也包含成功编译所需的源目录中的类. (3认同)

Moh*_*eem 8

我也遇到过类似的情况。我们需要仅将修改后的文件热交换到远程 Docker 容器,以缩短更改部署时间。我们就是这样解决的。

使用命令行参数在构建插件中添加包含选项。请注意,由于我们要添加多个文件,因此我们使用了include而不是include

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <compilerVersion>1.8</compilerVersion>
                <source>1.8</source>
                <target>1.8</target>
                <includes>${changed.classes}</includes>
            </configuration>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

现在使用参数运行编译阶段,例如:

mvn compile -Dchanged.classes=com/demo/ClassA.java,com/demo/ClassB.java,com/demo2/*
Run Code Online (Sandbox Code Playgroud)