AspectJ Maven插件无法编译我的项目

dmi*_*ony 17 java aspectj java-ee maven

我尝试使用aspectj maven插件与aspectj编译器编译项目,然后我尝试将类打包成"war"文件.不幸的是,它不适用于以下配置(pom.xml):

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.liferay.maven.plugins</groupId>
            <artifactId>liferay-maven-plugin</artifactId>
            <version>${liferay.maven.plugin.version}</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                </execution>
            </executions>
            <configuration>
                <autoDeployDir>${liferay.auto.deploy.dir}</autoDeployDir>
                <appServerDeployDir>${liferay.app.server.deploy.dir}</appServerDeployDir>
                <appServerLibGlobalDir>${liferay.app.server.lib.global.dir}</appServerLibGlobalDir>
                <appServerPortalDir>${liferay.app.server.portal.dir}</appServerPortalDir>
                <liferayVersion>${liferay.version}</liferayVersion>
                <pluginType>portlet</pluginType>

            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <source>1.7</source>
                <target>1.7</target>
                <showWarnings>true</showWarnings>
                <failOnError>true</failOnError>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilationLevel>1.7</compilationLevel>
                <encoding>UTF-8</encoding>
            </configuration>
            <executions>
                <execution>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.7.4</version>
    <type>jar</type>
</dependency>
Run Code Online (Sandbox Code Playgroud)

之后mvn clean install我看到下列情况除外:

[INFO] --- aspectj-maven-plugin:1.7:compile (default) @ tvbs-portlet ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[ERROR] Missing message: configure.incompatibleComplianceForSource in: org.aspectj.ajdt.ajc.messages
    <unknown source file>:<no line information>

[ERROR] no sources specified
    <unknown source file>:<no line information>

[ERROR] AspectJ Compiler 1.8.2

    Usage: <options> <source file | @argfile>..

AspectJ-specific options:
    -inpath <list>      use classes in dirs and jars/zips in <list> as source
Run Code Online (Sandbox Code Playgroud)

有人能建议我解决一下吗?

cod*_*ion 14

这似乎是一个已知的问题http://jira.codehaus.org/browse/MASPECTJ-125

您可以通过将以下内容添加到您的pom文件来修复它.

<complianceLevel>1.6</complianceLevel>
Run Code Online (Sandbox Code Playgroud)

  • 刚刚对合规级别1.8进行了测试,我可以验证它确实为我修复了错误. (6认同)

kri*_*aex 7

更新:虽然我在这个答案中所说的关于AspectJ Maven配置的内容都是正确的,但是我手边的具体问题的根本原因 - 糟糕的Maven依赖管理 - 在我的另一个答案中有所描述.如果那个是接受的答案而不是这个答案会更好.


  • 用户codelion的提示是有道理的,请更改你的<compilationLevel>标签(错字?) - 到<complianceLevel>.
  • 没有必要降级到插件版本1.6,你可以保持1.7.
  • 在该部分中也没有必要再次指定配置<execution>,插件级别的配置就足够了.
  • 请注意,插件1.7中的默认AspectJ版本是1.8.2,所以也许您对1.7.4的运行时依赖性有效,但如果我是你,我也会升级那个,最佳地与插件版本同步.这不是硬性要求,但我认为这是有道理的.
  • 也许你甚至想在插件和运行时升级到当前版本的AspectJ 1.8.4.这也可以通过向插件配置添加所需aspectjtools版本的依赖项来实现:
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.source-target.version>1.8</java.source-target.version>
        <aspectj.version>1.8.4</aspectj.version>
    </properties>

    <build>
        <pluginManagement>
             <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.7</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>UTF-8</encoding>
                        <verbose>true</verbose>
                    </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>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectj.version}</version>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>
    </dependencies>
Run Code Online (Sandbox Code Playgroud)


kri*_*aex 5

看了你的Maven项目https://github.com/dmitrievanthony/test-aspectj我发现了

  • 这个问题与AspectJ Maven Plugin完全无关,
  • Maven编译器插件中也会出现相同的编译错误
  • 你问题的根本原因就是糟糕的依赖管理.

下面是截图(全尺寸这里距离的IntelliJ IDEA的"发现类"):

在项目中找到类LockModeType 3x

如您所见,类LockModeType在3(三个!)依赖项中找到,其中一个包含不包含预期枚举值的类的版本.如果删除此依赖项,则会编译代码.

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>ejb3-persistence</artifactId>
        <version>1.0.2.GA</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

也许你应该清理你的依赖项.你可以使用Maven Dependency插件具有类似目标dependency:analyze,并dependency:tree为此目的.