简体   繁体   English

如何使AspectJ和Maven参与战争项目?

[英]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. 我有一个使用标准Maven war布局编译为webapp的maven项目。 I am trying to add aspects to the same project but the aspects are not triggered when deployed as a war on Tomcat. 我正在尝试将方面添加到同一项目中,但是当作为对Tomcat的战争部署时,这些方面不会触发。 If I deploy the project as a jar, the aspects kick in. 如果我将项目部署为jar,则各个方面都会加入。

Here is how my pom.xml looks like 这是我的pom.xml的样子

``` ```

<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. 该方面在项目的一个程序包中定义为带有@Aspect批注的JavaClass。

What could be I doing wrong? 我可能做错了什么?

To create a war, I do mvn clean install and move the war to the webapps directory. 为了产生战争,我执行mvn clean install并将战争移至webapps目录。 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. 为了创建一个Jar,我将装配插件与Main Class一起使用,这不会启动HTTP服务,但会启动代码中的其他进程。

Make sure you have a property called war.bundle 确保您拥有一个名为war.bundle的属性

true 真正

Have a look on http://maven.apache.org/maven-1.x/plugins/aspectj/ 看看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 如果您没有弹簧运行,则可能需要aop.xml,如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. 部署在Tomcat(或据我所知的任何其他Web容器)上的Wars通过反射过程调用了其方法,并且这种方式不会触发“ call()”切入点。 Try switching your "call()" to "execution()", worked for me on a Jonas with maven handling the deployment via cargo. 尝试将您的“ call()”切换为“ execution()”,这在乔纳斯(Jonas)上为我工作,而maven通过货物处理部署。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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