0

I am trying to create an advice of a method of a project (test5) being called from another project (testMaven). I already included the project test5 as dependency in testMaven's pom.xml and set up an advice in testMaven's class but still it doesn't get executed. The calling of the method (dfg) is working just fine.

Here are the code for it:

MainApp.java (testMaven)

package testMaven;

import test5.yxc;

public class MainApp {

    public static void main(String[] args) {
        yxc b = new yxc();
        b.dfg(2);
    }
}

yxc.java (test5)

package test5;

public class yxc {

    public void dfg(int a){
        System.out.println(a);

    }
}

testAspect.java (testMaven)

package testMaven;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import test5.yxc;

@Aspect
public class testAspect {

    @Before("execution(* test5.yxc.dfg(..))")
    public void testBefore(){
        System.out.println("yooi");
    }
}

pom.xml(testMaven)

<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>testingMaven</groupId>
    <artifactId>testMaven</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <spring.version>4.2.5.RELEASE</spring.version>
        <java.version>1.8</java.version>
        <!-- Maven Plugin Versions -->
        <maven.compiler.plugin.version>3.2</maven.compiler.plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>test5</groupId>
            <artifactId>test5</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

    <build>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.8</version>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>

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

Could you help me what's wrong with this? Thanks

Ihsan Haikal
  • 1,085
  • 4
  • 16
  • 42

1 Answers1

1

Checked your code in testMaven and checked out the code and changed the POM as follows:

<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>testMaven</groupId>
<artifactId>testMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>

    <java.version>1.8</java.version>
    <aspectj.version>1.8.9</aspectj.version>
    <!-- Maven Plugin Versions -->
    <maven.compiler.plugin.version>3.2</maven.compiler.plugin.version>

</properties>

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

<build>

    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.8</version>
            <configuration>
                <complianceLevel>${java.version}</complianceLevel>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <showWeaveInfo>true</showWeaveInfo>
            </configuration>
            <executions>
                <execution>
                    <id>AspectJ-Classes</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>AspectJ-Test-Classes</id>
                    <phase>process-test-classes</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
        </plugin>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>

</build>

NOTE: Added assembly plugin to make testing/verifying for myself easier.

When I compile the project I see within the maven logs the following entry:

[INFO] --- aspectj-maven-plugin:1.8:compile (AspectJ-Classes) @ testMaven ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[INFO] Join point 'method-execution(void testMaven.aaaa.aa(int))' in Type 'testMaven.aaaa' (aaaa.java:5) advised by before advice from 'testMaven.aspecter' (aspecter.java:10)

When I now execute the jar as follows I see the following result:

...>java -cp target/testMaven-0.0.1-SNAPSHOT-jar-with-dependencies.jar testMaven/MainApp
yooi
2

Given the MainApp is as follows:

package testMaven;

public class MainApp {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        aaaa a = new aaaa();
        a.aa(2);
    }

}

The class that is initiated prints the passed argument

package testMaven;

public class aaaa {

    public void aa(int a){
        System.out.println(a);
    }
}

And the interceptor as follows

package testMaven;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;


@Aspect
public class aspecter {
    @Before("execution(*  testMaven.aaaa.aa(..))")
    public void testBefore(){
        System.out.println("yooi");
    }
}

I would say it works :)

uniknow
  • 938
  • 6
  • 5
  • It still doesn't give me the advice using your advice on plugin (btw i didn't use the execution part as there is some error with the tools.jar, trying to fix it but still no luck) – Ihsan Haikal Apr 08 '16 at 12:50
  • Do you see the aspectj compiler doing something and showing log entries like i mentioned above? – uniknow Apr 08 '16 at 13:46
  • And do you see something like this `aspectj-maven-plugin:...:...` in your maven output (please put it in your response what you see exactly). PS: what is the problem with `tools.jar`? – uniknow Apr 08 '16 at 13:52
  • I myself use aspectj version 1.8.7 and maven plugin 1.7 of aspectj – uniknow Apr 08 '16 at 13:54
  • No there isn't even aspectj-maven-plugin during maven install, only maven resources, compiler plugin, resources plugin, compiler plugin, surefire plugin, jar plugin and install plugin. The error is [ERROR] 'dependencies.dependency.systemPath' for com.sun:tools:jar must specify an absolute path but is $ {toolsjarSystemPath} @ – Ihsan Haikal Apr 08 '16 at 13:58
  • Well the error that I mentioned above was in pom.xml and when I tried including the execution and goals it gave me error [ERROR] Syntax error, annotations are only available if source level is 1.5 or greater C:\Users\Muhammad.Sukmana\workspace\testMaven6\src\main\java\testMaven6\aspecter.java:11 @Aspect ^^^^^^. Do you know why? – Ihsan Haikal Apr 08 '16 at 14:26
  • Could you include the following properties into your maven pom. ` 1.81.8`. That should make sure that you are compiling for java 1.8. – uniknow Apr 08 '16 at 15:37
  • Regarding your other feedback. Yes for me aspectj is working with maven. It seems that you are suffering from other problems than aspectj only. Please make sure that you include the execution part of the aspectj plugin. Are you sure your java home is referencing the proper JDK? – uniknow Apr 08 '16 at 15:39
  • hmm I fixed those problems but still it seems that it does not work as the aspectj maven plugin is not installed without any information whatsoever. Is it possible that you take a look into my (new but still same) project and tell me what's wrong? – Ihsan Haikal Apr 10 '16 at 14:29
  • also are you using spring alongside maven and aspectj, maven converted into aspectj so that it runs on aspectj/java compiler or just pure aspectj with the same maven listed in pom.xml? – Ihsan Haikal Apr 10 '16 at 15:45
  • Please include the execution part also. If you want to use aspectj with maven you require the following in your spring configuration: An example of aspectj (use it for validating) in spring you ca find at [dbc4java](https://github.com/UniKnow/AgileDev/tree/develop/dbc4java). An example of compile time weaving can be found here [tutorial dbc](https://github.com/UniKnow/AgileDev/tree/develop/tutorials/dbc). In both project is use aspectj to intercept calls to methods which are annotated with jsr303 annotations. – uniknow Apr 10 '16 at 16:23
  • is it possible to use aspectj in maven without spring? I'm not proficient using Spring and thinking whether it's possible to run this project without Spring. Also I found another problem during installation of aspectj maven plugin with as it is failed to read artifact descripto and 'dependencies.dependency.systemPath' for com.sun:tools:jar must specify an absolute path but is ${toolsjarSystemPath} @. I tried using [this](http://stackoverflow.com/questions/32997222/error-upgrading-aspectj-maven-plugin-v1-8) and [this](http://stackoverflow.com/questions/8375423/missing-artifact-com-suntoolsjar) – Ihsan Haikal Apr 10 '16 at 16:43
  • but still gave me an error for it, btw this is the project of standalone for [testMaven](https://github.com/ihsanhaikalz/testMaven) a little bit different. If you could take a look into my code, that would be helpful – Ihsan Haikal Apr 10 '16 at 19:30
  • thanks for the code review, I tried it but when I tried to Maven Install the project it still doesn't give me the INFO messages like you have above. Also it gave me an error for the execution goals as "Plugin execution not covered by lifecycle configuration" but I tried using [this](http://stackoverflow.com/questions/6352208/how-to-solve-plugin-execution-not-covered-by-lifecycle-configuration-for-sprin) and worked but still no luck of running the AspectJ in Maven. – Ihsan Haikal Apr 11 '16 at 09:25
  • Which maven/jdk version are you using? – uniknow Apr 11 '16 at 09:36
  • I tried using different computer for doing this and now basically it works. I think there are some plugins or parts that were triggered during importing file that makes it work. Thank you very much for your help! Already accept your comment as the answer. – Ihsan Haikal Apr 11 '16 at 14:28
  • Sounds a bit strange that it only works for certain computers, but thanks for accepting the answer – uniknow Apr 11 '16 at 14:30
  • I found again this error of not finding the com:sun:tools.jar and I already put the question [here](http://stackoverflow.com/questions/37994466/dependency-error-aspectj-maven-plugin-installation-for-comsuntoolsjar) when I tried to run the same project on different computer. I can't remember what fixed it and still have this error. Could youtake a look at this question? Thanks – Ihsan Haikal Jun 23 '16 at 15:15