19

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

有人可以建议我一些解决方案吗?

4

5 回答 5

16

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

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

<complianceLevel>1.6</complianceLevel>
于 2014-11-11T11:08:58.553 回答
9

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


  • 用户 codelion 的提示是有道理的,请将您的<compilationLevel>标签(错字?)更改为<complianceLevel>.
  • 不需要降级到插件版本 1.6,可以保留 1.7。
  • 也不需要在<execution>section 中再次指定配置,插件级别的配置就足够了。
  • 请注意,插件 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>
于 2014-11-11T14:08:39.563 回答
5

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

  • 该问题与 AspectJ Maven 插件完全无关,
  • 同样的编译错误也发生在 Maven Compiler Plugin 和
  • 你的问题的根本原因只是糟糕的依赖管理。

这是 IntelliJ IDEA 的“查找类”的屏幕截图(此处为全尺寸):

在项目中找到 3x 类 LockModeType

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

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

也许你应该清理你的依赖项。您可以将 Maven 依赖插件与类似的目标一起dependency:analyze使用dependency:tree

于 2014-11-12T12:38:18.333 回答
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插件参数?

于 2014-11-11T12:19:16.773 回答
0

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

于 2016-12-23T02:50:52.023 回答