java - spectj-maven-plugin,声明soft如何编译

标签 java maven aspectj aspectj-maven-plugin

是否可以仅使用aspectj-maven-plugin来编译具有软化异常(例如:declare soft:Exception:execution(* *.*());)方面的项目?我无法处理它......我仍然收到编译错误

unreported exception Exception; must be caught or declared to be thrown

所以它是在没有考虑各个方面的情况下编译的。

我正在使用这个命令来编译:

mvn clean aspectj:compile

我的 pom.xml 是:

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>pl.group.id</groupId>
<artifactId>aop</artifactId>
<version>1.0.0</version>

<dependencies>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.7.4</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
    </dependency>


</dependencies>

<build>

    <plugins>

        <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>
                <aspectDirectory>src/main/aspect</aspectDirectory>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>
</project>

我做错了什么?

最佳答案

实际上,我认为 Maven 编译器插件 3.0 和 3.1 中有一个错误,因为对我来说它可以与插件版本 2.5.1 一起使用。增量编译似乎被破坏了,或者相应的开关 useIncrementalCompilation 自从在 3.1 中引入以来,其含义不知何故意外颠倒了。有趣的是,将其值设置为 false 可以解决我的问题。降级到 2.5.1 也可以。在这两种情况下,我都需要将 AspectJ Maven 插件的阶段更改为 process-sources,如用户 @Gus 链接的问题中所述。

解决方案A:

<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-soft-exceptions</artifactId>
    <version>1.0-SNAPSHOT</version>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <aspectj.version>1.8.0</aspectj.version>
</properties>

<name>AspectJ sample with declare soft</name>
<description>Demonstrates how to build an AspectJ project using "declare soft" with Maven</description>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <!-- IMPORTANT -->
                <useIncrementalCompilation>false</useIncrementalCompilation>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.6</version>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
                <source>1.7</source>
                <target>1.7</target>
                <Xlint>ignore</Xlint>
                <complianceLevel>1.7</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>
</build>

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

</project>

解决方案B:

<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-soft-exceptions</artifactId>
    <version>1.0-SNAPSHOT</version>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <aspectj.version>1.8.0</aspectj.version>
</properties>

<name>AspectJ sample with declare soft</name>
<description>Demonstrates how to build an AspectJ project using "declare soft" with Maven</description>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <!-- IMPORTANT - or remove this plugin config altogether because 2.5.1 is the default in Maven 3 anyway -->
            <version>2.5.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.6</version>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
                <source>1.7</source>
                <target>1.7</target>
                <Xlint>ignore</Xlint>
                <complianceLevel>1.7</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>
</build>

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

</project>

关于java - spectj-maven-plugin,声明soft如何编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24211943/

相关文章:

java - ClasscastException : MyClass cannot be cast to javax. servlet.Filter

maven - Gradle 构建失败,并显示“无法解析...${javassist.version}...路径中存在非法字符”

java - 使用方面进行日志记录

java - 为什么 pointcut.matches(String.class) 返回 "true"

java - Spring @Repository 最佳实践

java - 更新属性文件中的属性值而不删除其他值

java - Google App Engine 项目可以在开发环境中运行,但在部署时不能运行

java - 如何在多个私有(private)连接位置更快地构建依赖项

maven - Intellij Idea中jar内容的调试

java - 使用 Gradle AspectJ 将生产方面编织到测试类中