Maven:在编译中排除java文件

时间:2013-07-29 09:53:12

标签: maven

我有一个java源文件夹,我希望从编译中排除。

我的文件夹位于qa/apitests/src/main/java/api/test/omi

我在pom.xml下的qa/bamtests添加了以下条目,但没有帮助。除此之外我还需要一个条目吗?

   <build>
     <resources>
       <resource>
         <directory>src/main/java</directory>
         <includes>
           <include>**/*.properties</include>
           <include>**/*.xml</include>
           <include>**/*.xsd</include>
           <include>**/*.csv</include>
         </includes>
         <excludes>
<exclude>src/main/java/api/test/omi</exclude>
         </excludes>
       </resource>
</build>

5 个答案:

答案 0 :(得分:53)

使用Maven Compiler Plugin

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <excludes>
      <exclude>**/api/test/omi/*.java</exclude>
    </excludes>
  </configuration>
</plugin>

答案 1 :(得分:40)

添加排除作为建议的其他答案对我有用,除了路径不应包含“src / main / java”:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <excludes>
          <exclude>com/filosync/store/StoreMain.java</exclude>
        </excludes>
      </configuration>
    </plugin>
  </plugins>
</build>

答案 2 :(得分:20)

对于任何需要排除测试来源 <exclude>标记的人都无效。您需要使用<testExclude>代替:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
      <testExcludes>
        <testExclude>**/PrefixToExclude*</testExclude>
      </testExcludes>
    </configuration>
  </plugin>

答案 3 :(得分:3)

如果要从编译中排除java源代码,请在Maven Compiler Plugin定义中提及它们

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
            <excludes>
              <exclude>src/main/java/api/test/omi/*.java</exclude>
            </excludes>
        </configuration>
    </plugin>
</plugins>

资源插件仅定义要在最终工件中捆绑的所有资源。

答案 4 :(得分:1)

最高投票的答案可以正常运行,但当被排除的类/类被未被排除的类使用时,它不允许强制排除。

使用maven-antrun-plugin解决方法:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <phase>process-classes</phase>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <tasks>
            <delete dir="target/classes/folder/to/exclude"/>
        </tasks>
    </configuration>
</plugin>