0

I am trying to reproduce the example in Using AspectJ annotations in maven project: weaving is not working with the answer given there. In articular, now I am using Eclipse Photon, JDK 1.8 but with no success.

My Final POM.XML is:

<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</groupId>
<artifactId>aspect-tutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
    <maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.5</version>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.11</version>
                <configuration>
                    <complianceLevel>1.8</complianceLevel>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
                <executions>
                    <execution>
                        <!-- IMPORTANT -->
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <!-- IMPORTANT -->
                    <useIncrementalCompilation>false</useIncrementalCompilation>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <mainClass>com.pkg.MainApp</mainClass>
                </configuration>
            </plugin>

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

I have tried to run both with Eclipse (MainApp --> Run as...) and with Maven (pom.xml -->Run as --> Maven Build), but no weaving is taken place. Any idea?

Thanks a lot! Bea

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • On first sight the POM looks okay, it is pretty similar to my answer in the other question where you have taken it from. I think the problem could be in your directory structure and in your code. The best thing would be to publish your whole project (reduced to an [MCVE](https://stackoverflow.com/help/mcve), if it is big) on GitHub and share a link, so I can clone and analyse it. Like this I cannot tell you what is going wrong, I don't even see your application and aspect code. P.S.: Did you install AspectJ Development Tools (AJDT) in Eclipse? – kriegaex Dec 31 '19 at 04:24
  • Thanks Kriegaex. Yes, it works if I convert the project into an AspectJ one. However, I am interested in performing the weaving using travis CI. In Eclipse the weaving is performed thanks to AJDT, but I would like to perform the weaving in travis. I have a public toy example in GitHug (https://github.com/beperev/aspectjExample) connected with travis ( https://travis-ci.com/beperev/aspectjExample). I can see that both tests are passed but no "Hello" msg is shown from the "before" advice. – Bea Pérez Valle Dec 31 '19 at 10:45
  • You completely changed the focus of the question. This was about Maven and AspectJ, not about Eclipse or Travis CI. If your Maven build works, everything should fall into place because you can import Maven projects in IDEs like Eclipse and IntelliJ IDEA, run Maven builds on Travis CI etc. I see no reason why it would not work there. – kriegaex Jan 01 '20 at 04:38

1 Answers1

1

I forked your repo and fixed your Maven POM. You actually made a Maven beginner's mistake by pre-configuring plugins in the <pluginManagement> section but never actually declaring usage of the pre-configured plugins in the <plugins> section. In that case of course Maven does not use the AspectJ Maven plugin and thus does not weave any aspects either.

I sent you a pull request which you can just accept in order to make this work. There are several other things in your Maven settings which could use improvement, e.g.

  • if you really want to use AspectJ 1.9.5 and not the preconfigured 1.8.13 used in AspectJ Maven, there is a way to achieve that, but not the way you did it. Feel free to ask for more details if this is an issue.

  • if you want to use a JDK newer than 8 in order to run your Maven build. In that case you need to switch to a fork of AspectJ Maven because version 1.11 does not work with JDK 9+. I can also give you more details there if necessary. But with JDK 8 you should be fine.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • You're right. I tried so many things that I posted a wrong pom.xml file. Now it works perfectly. Just for others to know, I have included the `jdk: - openjdk8` in the travis.yml file to use JDK8. Thanks again! – Bea Pérez Valle Jan 02 '20 at 21:35