2

I am trying to compile a very trivial Helloworld java program with AspectJ using Maven. Even though it works within eclipse, when I create a jar file and try to run from command line, the aspect advices are not executed at all.

Here is my java program:

public class HelloWorld {

  /**
   * @param args
   */
  public static void main(String[] args) {
    HelloWorld world = new HelloWorld();
    world.greet();
    System.out.println("Hello you stupid world");
  }

  private void greet() {
    System.out.println("Hello foolish world!!!");
  }
}

My aspect file: LogAspect.aj

package hello;

public aspect LogAspect {   
  pointcut abc() : execution (* hello.HelloWorld.main(..));

  before() : abc() {
    System.out.println("Aspect:Executing HelloWorld method...");
  }

  after() : abc() {
    System.out.println("Aspect:Finished executing a Helloworld method.");
  }
}

This is my pom.xml file:

<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>com.comviva.hell</groupId>
<artifactId>hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello</name>
<description>world</description>
<packaging>jar</packaging>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.7</version>
                <configuration>
                    <complianceLevel>1.7</complianceLevel>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                        <configuration>
                            <!-- <complianceLevel>1.7</complianceLevel> -->
                            <source>1.7</source>
                            <target>1.7</target>
                        </configuration>
                        <!-- IMPORTANT -->
                        <phase>process-sources</phase>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>hello.HelloWorld</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>hello.HelloWorld</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.4</version>
        <!-- <scope>compile</scope> -->
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.7.4</version>
    </dependency>
</dependencies>

This is the output when running through Eclipse (which is correct)

Aspect:Executing HelloWorld method...

Hello foolish world!!!

Hello you stupid world

Aspect:Finished executing a Helloworld method.

However, when I build jar using maven and run the resulting jar, I don't get the aspect advice outputs at all.

D:\projects\hell\target>java -jar hello-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Hello foolish world!!!
Hello you stupid world

D:\projects\hell\target>

Thanks to anyone helping me out on this.

EDIT: After trying suggestion to move maven-plugin out of

Now, I have removed the tags. But I get the error as shown below. screenshot showing pom error

Community
  • 1
  • 1
Mopparthy Ravindranath
  • 3,014
  • 6
  • 41
  • 78
  • If I remove it out of pluginManagement, I get an error for the pom saying "Plugin execution not covered by lifecycle configuration". Last time when I had this kind of error, I looked up to find somebody suggesting to put the "pluginManagement". – Mopparthy Ravindranath Apr 09 '15 at 13:46
  • Yes, it works, in spite of IDE error markers. Initially I didn't attempt to build, thinking that it will fail, since there are errors shown in pom. – Mopparthy Ravindranath Apr 10 '15 at 03:36
  • Possible duplicate of [How to solve "Plugin execution not covered by lifecycle configuration" for Spring Data Maven Builds](https://stackoverflow.com/questions/6352208/how-to-solve-plugin-execution-not-covered-by-lifecycle-configuration-for-sprin) – Gaurav Jeswani Sep 03 '19 at 08:26

1 Answers1

0

I am not really answering my own question. Just acknowledging that the suggestions given by khmarbaise and sheltem worked for me:

That is just a problem of the m2e plugin, you can use one of the two quick fixes marked with a red x to get rid of it. If you don't want IDE specific stuff in your pom, I'd suggest using the second (middle) quick fix option. You should be able to build via maven despite that anyway, does your aspect work now that you are actually using the aspectj-maven-plugin in your build? – sheltem Apr 9 '15 at 14:13

and:

You need to move the definition of aspectj-maven-plugin out of pluginManagement. pluginManagement defines only the versions and configuration for plugins but does not let them being executed. – khmarbaise Apr 9 '15 at 13:36

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Mopparthy Ravindranath
  • 3,014
  • 6
  • 41
  • 78