简体   繁体   中英

How to get AspectJ and maven working on a War project?

I have a single maven project, which compiles to webapp, with the standard Maven war layout. I am trying to add aspects to the same project but the aspects are not triggered when deployed as a war on Tomcat. If I deploy the project as a jar, the aspects kick in.

Here is how my pom.xml looks like

```

<groupId>in.sheki</groupId>
<artifactId>abc-service</artifactId>
<packaging>war</packaging>
<name>abc-service</name>

<properties>
    <aspectj.version>1.6.12</aspectj.version>
</properties>

<build>
    <finalName>abc-service</finalName>

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.4</version>
            <configuration>
                <complianceLevel>1.6</complianceLevel>
            </configuration>
            <executions>
                <execution>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
   <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectj.version}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${aspectj.version}</version>
    </dependency> 
   ....
 </dependencies>
 </project>

```

The aspect is defined in one of the packages of the project as a JavaClass with @Aspect annotation.

What could be I doing wrong?

To create a war, I do mvn clean install and move the war to the webapps directory. For creating a Jar, I use the assembly plugin with a Main Class, this does not start the HTTP services but starts the other processes in my code.

Make sure you have a property called war.bundle

true

Have a look on http://maven.apache.org/maven-1.x/plugins/aspectj/

If you are running without spring then you may require aop.xml as described in http://ganeshghag.blogspot.in/2012/10/demystifying-aop-getting-started-with.html

Wars deployed on Tomcat (or any other web container as far as I know) have their methods called through the relection process, and that way does not trigger the "call()" pointcut. Try switching your "call()" to "execution()", worked for me on a Jonas with maven handling the deployment via cargo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM