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

时间:2014-11-11 11:05:41

标签: java maven java-ee aspectj

我尝试使用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>

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

有人可以建议我解决一下吗?

5 个答案:

答案 0 :(得分:13)

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

您可以通过在pom文件中添加以下内容来修复它。

<complianceLevel>1.6</complianceLevel>

答案 1 :(得分:7)

更新:虽然我在这个答案中所说的有关AspectJ Maven配置的内容都是正确的,但是我手头的问题 - {3中描述了手头的具体问题的根本原因 - 糟糕的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>

答案 2 :(得分:5)

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

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

以下是IntelliJ IDEA的截图(完整尺寸here)&#34;查找课程&#34;:

Class LockModeType is found 3x in the project

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

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>ejb3-persistence</artifactId>
        <version>1.0.2.GA</version>
    </dependency>

也许你应该清理你的依赖项。您可以将Maven Dependency Plugin与dependency:analyzedependency:tree等目标一起用于此目的。

答案 3 :(得分:1)

在将插件配置更改为以下后,它将起作用:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.6</version>
    <configuration>
        <complianceLevel>1.7</complianceLevel>
        <source>1.7</source>
        <target>1.7</target>
        <encoding>UTF-8</encoding>
    </configuration>
    <executions>
        <execution>
            <phase>process-sources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <complianceLevel>1.7</complianceLevel>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </execution>
    </executions>
</plugin>

但在此之后我收到了很多不同的编译错误:

[ERROR] Failed to execute goal org.codehaus.mojo:aspectj-maven-plugin:1.6:compile (default) on project tvbs-portlet: Compiler errors:
[ERROR] error at Entitle.class, entitleId, LockModeType.PESSIMISTIC_WRITE);
[ERROR]
[ERROR] /Users/<...>/ejb/BillingEJB.java:43:0::0 PESSIMISTIC_WRITE cannot be resolved or is not a field
[ERROR] error at .createQuery("select e from Entitle e " +
[ERROR]
[ERROR] /Users/<...>/ejb/EntitleEJB.java:62:0::0 The method createQuery(String) in the type EntityManager is not applicable for the arguments (String, Class<Entitle>)
[ERROR] error at return entityManager.createQuery(
[ERROR] ^^

可能导致的aspectj插件参数不正确吗?

答案 4 :(得分:0)

确保模块有源代码,如* .java等。 当我在版本4.0.6上编译CAS时发生了这个错误,我发现cas-server-uber-webapp在src文件夹中没有任何源代码。只需从父pom.xml中删除该模块。