【问题标题】:Maven + AspectJ weaving for Java8用于 Java8 的 Maven + AspectJ 编织
【发布时间】:2017-01-14 04:30:55
【问题描述】:

我不能 mvn package 使用下面的最小示例。 Eclipse(Mars.2 Release 4.5.2)编译和编织没有问题。

我必须做些什么才能让它发挥作用?

输出:

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 2 source files to ...\workspace\test\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] .../workspace/test/src/main/java/test/Foo.java:[6,21] cannot find symbol
  symbol: method doSomethingInjected()

一个示例类:

package test;

public class Foo {
    public void bar() {
        this.doSomethingInjected();
    }
}

示例界面:

package test;

public interface Injectable { }

方面:

package test;

public aspect Injection {

    declare parents : test..* implements Injectable;

    public void Injectable.doSomethingInjected() {
        System.out.println("done");
    }
}

pom.xml(相关部分按照aspectj-maven-plugin usage doc

<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>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.9</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.9</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

    标签: maven java-8 aspectj aspectj-maven-plugin


    【解决方案1】:

    试试这个,它会让你的项目编译和运行干净(我测试过):

    <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>de.scrum-master.stackoverflow</groupId>
      <artifactId>aspectj-introduce-method</artifactId>
      <version>1.0-SNAPSHOT</version>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.source-target.version>1.8</java.source-target.version>
        <aspectj.version>1.8.10</aspectj.version>
      </properties>
    
      <build>
    
        <plugins>
    
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
              <source>${java.source-target.version}</source>
              <target>${java.source-target.version}</target>
              <!-- IMPORTANT -->
              <useIncrementalCompilation>false</useIncrementalCompilation>
            </configuration>
          </plugin>
    
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.9</version>
            <configuration>
              <!--<showWeaveInfo>true</showWeaveInfo>-->
              <source>${java.source-target.version}</source>
              <target>${java.source-target.version}</target>
              <Xlint>ignore</Xlint>
              <complianceLevel>${java.source-target.version}</complianceLevel>
              <encoding>${project.build.sourceEncoding}</encoding>
              <!--<verbose>true</verbose>-->
              <!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn>-->
            </configuration>
            <executions>
              <execution>
                <!-- IMPORTANT -->
                <phase>process-sources</phase>
                <goals>
                  <goal>compile</goal>
                  <goal>test-compile</goal>
                </goals>
              </execution>
            </executions>
            <dependencies>
              <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjtools</artifactId>
                <version>${aspectj.version}</version>
              </dependency>
            </dependencies>
          </plugin>
    
        </plugins>
    
      </build>
    
      <dependencies>
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjrt</artifactId>
          <version>${aspectj.version}</version>
          <scope>runtime</scope>
        </dependency>
      </dependencies>
    
    </project>
    

    【讨论】:

    • 两个问题:如果我的方面在 test-jar 中,我将如何在 maven 中配置它?其次,为什么有两行标记为 ?我无法找到任何解释为什么这些是必要的。
    • 问题 #2:这些是给我自己的笔记,因为要使用增量编译,您必须将其设置为 false,请参阅 bug ticket 获取 Maven 编译器。如果使用 Maven 编译器编译其他代码,将 AspectJ Maven 的阶段设置为“进程源”有助于避免某些问题。默认阶段是“编译”,但在大多数情况下(更早),“进程源”更好。我不想在评论中详细介绍。
    • 问题 #1:您可以将 test-jar 添加为要在其中使用它的模块的测试依赖项,然后从 AspectJ Maven 的 aspectLibraries 部分引用它。然后您可能会为compiletest-compile 定义两个单独的插件配置/执行,以避免您的测试方面最终进入生产环境。但我不确定是否需要,我没有尝试过。不过有趣的问题。如果您还有其他问题,我建议您提出一个全新的问题,以便我们单独处理。
    • 这是我完整的 pom
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多