0

I am trying to create a custom aspectj annotation. I have following in pom:

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.5</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.5</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>1.9.5</version>
    </dependency>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.11</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>       
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

I get following error on running 'mvn clean install':

[ERROR] Failed to execute goal org.codehaus.mojo:aspectj-maven-plugin:1.11:compile (default) on project AspectTest: AJC compiler errors:
[ERROR] error at @Aspect
[ERROR] ^^^^^^
[ERROR] .....\MyMethodPerformanceAspect.java:7:0::0 Syntax error, annotations are only available
 if source level is 1.5 or greater

MyMethodPerformanceAspect is my annotation definition class.

I have jdk 8 in the system. I am using intellij, with compiler level and source level set to 8.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Mandroid
  • 6,200
  • 12
  • 64
  • 134
  • Do you have source/target levels defined in `pom.xml`? Maven defaults to 1.5 version unless you override it, please see my answer at https://stackoverflow.com/a/12900859/104891. Does it help? – CrazyCoder Jul 18 '20 at 18:52
  • I have all as per your answer. – Mandroid Jul 18 '20 at 19:01
  • Does the issue occur with the command line `mvn clean package` as well or only in IntelliJ IDEA? Can you show the complete `pom.xml`? – CrazyCoder Jul 18 '20 at 19:02

1 Answers1

1

You have a Maven problem, this is completely unrelated to IntelliJ IDEA. IDEA only imports the configuration from Maven.

If you have something like this in your POM

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
</properties>

you can re-use the Maven Compiler Plugin settings for AspectJ Maven Plugin too:

<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
  <source>${maven.compiler.source}</source>
  <target>${maven.compiler.target}</target>
  <complianceLevel>${maven.compiler.target}</complianceLevel>
  <encoding>${project.build.sourceEncoding}</encoding>
</configuration>

Fix your Maven POM, then re-import to IDEA and the problem will be fixed in both environments.

kriegaex
  • 63,017
  • 15
  • 111
  • 202