如何排除使用注释在maven中编译java类

时间:2013-02-18 15:03:05

标签: java maven annotations

我已经有一个工作解决方案,我可以使用maven指定在使用特定maven配置文件时哪些类无法编译。

但我想使用通用解决方案并使用注释

我目前的解决方案就像

<plugin>
    <!-- Exclude some web services used only for internal testing -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <optimize>true</optimize>
        <excludes>
            <exclude>**/something/*ClassPattern.java</exclude>
        </excludes>
        <testExcludes>
            <exclude>**/something/*ClassPatternTest.java</exclude>
        </testExcludes>
    </configuration>
</plugin>

但是有点像

@NotCompiledForProduction 

在课堂上会相当不错。

在我看来,如果不改变maven的行为,这可能很难(或者不可能)。这不是范围。而这种注释

2 个答案:

答案 0 :(得分:0)

你可以试试这个......

<build> <plugins>
  <!-- Run annotation processors on src/main/java sources -->
  <plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <executions>
      <execution>
        <id>process</id>
        <goals>
          <goal>process</goal>
        </goals>
        <phase>generate-sources</phase>
      </execution>
    </executions>
  </plugin>
  <!-- Disable annotation processors during normal compilation -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <compilerArgument>-proc:none</compilerArgument>
    </configuration>
  </plugin>
</plugins> </build>

答案 1 :(得分:0)

您不能(我假设)使用注释来确定将哪些源代码呈现给java编译器,因为您需要首先编译源代码以处理注释。

您似乎需要在maven项目中创建不同的模块:一个生成带有生产代码的jar文件,另一个模块生成一个带有测试实现的jar文件,并依赖于生产工件。

如果代码确实需要在同一个maven模块中,那么代码应始终进行编译。但是,您可以使用maven-jar-pluginpackage阶段创建多个工件:默认 artifactId。jar artifactId - test-lib.jar 工件。您可以通过为插件指定多个执行来执行此操作,并使用<includes><excludes>根据需要拆分jar文件。

相关问题