Maven:编译包含Java 1.6源代码的aspectj项目

时间:2010-04-09 20:19:28

标签: java maven-2 aop aspectj

主要问题

我想做的事情相当容易。或者你会想。但是,没有任何工作正常。

要求: 使用maven,使用AspectJ编译器编译Java 1.6项目。

注意: 我们的代码无法用javac编译。也就是说,如果未编入方面,则编译失败(因为我们有方面可以软化异常)。

<小时/> 2011年2月21日更新: 有两个同样可行的解决方案(两种情况都使用 aspectj-maven-plugin maven-compiler-plugin 结合使用):

  1. 添加     <failOnError>false</failOnError>     到编译器插件(谢谢     Pascal Thivent
  2. 添加<phase>process-sources</phase>     到aspectj编译器插件     (感谢Andrew Swan
  3. 有关这些解决方案的更多信息,请参阅答案部分。我认为解决方案#2是更好的方法。

    <小时/>




    相关问题

    问题(基于下面的失败尝试):

        
    1. 如何让maven运行aspectj:直接编译目标,而不运行compile:compile?
    2.   
    3. 你如何忽略compile:compile?
    4. 的失败   
    5. 如何指定指向您自己的ajc编译器的自定义compilerId(即make compile:compile使用除了plexus之外的aspectj编译器)?*
    6. 感谢您提出的所有建议。这些是我试过的导致我的问题/问题的事情:


      尝试1(失败): 指定aspectJ作为maven-compiler-plugin的编译器:

      <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.2</version>
      <configuration>
       <source>1.6</source>
       <target>1.6</target>
       <compilerId>aspectj</compilerId>
      </configuration>
      <dependencies>
       <dependency>
        <groupId>org.codehaus.plexus</groupId>
        <artifactId>plexus-compiler-aspectj</artifactId>
        <version>1.8</version>
       </dependency>
      </dependencies>
      </plugin>
      

      失败并显示错误:

      org.codehaus.plexus.compiler.CompilerException: The source version was not recognized: 1.6

      无论我使用的是什么版本的plexus编译器(1.8,1.6,1.3等),这都行不通。我实际上阅读了源代码,发现这个编译器不喜欢Java 1.5以上的源代码。

      尝试2(失败): 使用附加到编译和测试编译目标的aspectJ-maven-plugin:

      <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.3</version>
      <configuration>
       <source>1.6</source>
       <target>1.6</target>
      </configuration>
      <executions>
       <execution>
        <goals>
         <goal>compile</goal>      
         <goal>test-compile</goal> 
        </goals>
       </execution>
      </executions>
      </plugin>
      

      运行时失败:

      mvn clean test-compile
      mvn clean compile

      因为它在运行aspectj:compile之前尝试执行compile:compile。如上所述,我们的代码不能使用javac编译 - 这些方面是必需的。所以mvn需要跳过编译:完全编译目标并且只运行aspectj:compile。

      尝试3(有效但不可接受):

      使用上面相同的配置,而是运行:

      mvn clean aspectj:compile

      这很有效,因为它构建成功,但是我们需要能够直接运行编译目标和测试编译目标(m2eclipse自动构建取决于这些目标),这是不可接受的。此外,以这种方式运行将要求我们在整个过程中拼出我们想要的每个目标(例如,我们需要分配资源并运行测试以及部署测试资源等)

4 个答案:

答案 0 :(得分:16)

AspectJ插件的版本1.3 deliberately changed其编译目标的默认阶段,从“process-sources”到“compile”。要恢复之前在javac之前运行ajc的行为,您只需要在相关的“执行”标记中添加“阶段”标记,如下所示:

<execution>
    <phase>process-sources</phase> <!-- or any phase before compile -->
    <goals>
        <goal>compile</goal>
        <goal>test-compile</goal>
    </goals>
</execution>

答案 1 :(得分:4)

告诉maven-compiler-plugin跳过所有* .java文件并让aspectj-maven-plugin完成工作怎么样?

...
<build>
  <plugins>
    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.0.2</version>
      <configuration>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.3</version>
      <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <encoding>utf-8</encoding>
        <complianceLevel>1.6</complianceLevel>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>compile</goal>       <!-- weave main classes -->
            <goal>test-compile</goal>  <!-- weave test classes -->
          </goals>
        </execution>
       </executions>
    </plugin>
  </plugins>
</build>

答案 2 :(得分:2)

我在项目中使用此配置来使用Maven编译AspectJ和Java 6:

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.6.8</version>
    </dependency>
</dependencies>

<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>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.3</version>
            <configuration>
                <complianceLevel>1.6</complianceLevel>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>  
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

参考:aspectj-maven-plugin

答案 3 :(得分:1)

  

如何指定指向您自己的ajc编译器的自定义compilerId(即make compile:compile使用除了plexus之外的aspectj编译器)?

我不知道如何指定另一个compilerId而不是"official"。不确定是否可能。

我的理解是http://jira.codehaus.org/browse/MCOMPILER-107可以解决您的问题(AspectJ 1.6+确实支持Java 1. 6对吧?)。可悲的是,它仍然是开放的。

  

你如何忽略compile:compile?

的失败

compiler:compileMaven Compiler plugin目标有一个failOnError可选参数,允许表明即使存在编译错误,构建是否会继续

<project>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <failOnError>false</failOnError>
          ...
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

对于上述问题,这可能是一个丑陋的解决方法。

  

如何让maven运行aspectj:直接编译目标,而不运行compile:compile?

问题是 compiler:compile compile 阶段绑定,并且您无法删除default lifecyle binding。所以可能还有另一个选择,但我能想到的唯一选择就是使用<packaging>pom<packaging>将所有关闭并手动重新绑定所有目标(至少对于这些阶段:{ {1}},process-resourcescompileprocess-test-resourcestest-compiletest)。示意性地,如下所示:

process-resources       resources:resources
compile                 aspectj:compile
process-test-resources  resources:testResources
test-compile            compiler:testCompile
test                    surefire:test
package                 ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war
install                 install:install
deploy                  deploy:deploy

这可能是另一个丑陋的解决方法。免责声明:未经测试但应该有效。

相关问题