1

Is it possible to compile project with softened Exceptions (e.g.: declare soft: Exception : execution(* *.*());) aspects in it using only aspectj-maven-plugin ? I can't handle it... I am still getting compilation error

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

So it is compiled without taking aspects in account.

I am using this command to compile:

mvn clean aspectj:compile

and my pom.xml is:

<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>

What am I doing wrong?

kriegaex
  • 63,017
  • 15
  • 111
  • 202
liosedhel
  • 518
  • 5
  • 14
  • Does this question help? http://stackoverflow.com/questions/2610633/maven-compile-aspectj-project-containing-java-1-6-source – Gus Jun 13 '14 at 18:45
  • yea, it seems to be what I need. Please post it as an answer if you want:) I dont't know how I missed it... – liosedhel Jun 13 '14 at 18:57

1 Answers1

2

Actually I think there is a bug in Maven Compiler Plugin 3.0 and 3.1 because for me it works with plugin version 2.5.1. It seems that incremental compilation is broken or the corresponding switch useIncrementalCompilation is somehow accidentally reversed in its meaning since it was introduced in 3.1. Funnily, setting its value to false fixes the situation for me. Downgrading to 2.5.1 also does. In both cases I need to change the phase for the AspectJ Maven Plugin to process-sources as described in the question linked to by user @Gus.

Solution 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>

Solution 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>
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Yes, changing execution phase of maven aspectj plugin solved the problem. – liosedhel Jun 21 '14 at 18:47
  • No, the execution phase is not enough in all cases, you also have to use `useIncrementalCompilation = false`. I have added my sample Maven + AspectJ project to http://jira.codehaus.org/browse/MCOMPILER-209 and https://jira.codehaus.org/browse/MCOMPILER-194 so as to get this fixed. – kriegaex Jun 25 '14 at 09:56
  • Updated links are [MCOMPILER-209](https://issues.apache.org/jira/browse/MCOMPILER-209) and [MCOMPILER-194](https://issues.apache.org/jira/browse/MCOMPILER-194), due to the fact that Codehaus no longer exists and Maven Compiler Plugin is now an Apache project. – kriegaex Jan 23 '17 at 23:12