aop.xml没被Maven选中

时间:2015-01-09 12:09:53

标签: maven maven-plugin aspectj aspectj-maven-plugin

当我们使用Maven文件时,

aop.xml无法打包spring-agent.jar文件。

我们使用Surefire来运行单元测试用例,使用jacoco来获取代码覆盖率。

我们未发现spring-agent存在任何问题,但aspects未在运行时调用。我们已将aop.xml放在META-INF文件夹下,但它没有被提取。

关于如何让Maven获取aop.xml文件并将其打包到META-INF文件夹中的任何想法或指示?

这是我的pom.xml文件

*编辑**

这是我的pom.xml ..我删除了一些东西

                <groupId>project-Web</groupId>
                <artifactId>project-Web</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <packaging>war</packaging>
                <name>project-Web</name>
                <properties>
                                <hibernate.version>4.2.3.Final</hibernate.version>
                                <spring.version>3.2.3.RELEASE</spring.version>
                                <spring.security.version>3.2.1.RELEASE</spring.security.version>
                                <jersey.version>1.8</jersey.version>
                                <project.dir>${basedir}</project.dir>
                                <project.libdir>${project.dir}/lib</project.libdir>
                                <skipTests>false</skipTests>

                </properties>
                <repositories>
                                <repository>
                                                <id>maven-repo</id>
                                                <url>http://maven.apache.org</url>
                                </repository>
                </repositories>
                <dependencies>
                                                <dependency>
                                                <groupId>commons-validator</groupId>
                                                <artifactId>commons-validator</artifactId>
                                                <version>1.3.1</version>
                                </dependency>
                                <dependency>
                                                <groupId>org.apache.ant</groupId>
                                                <artifactId>ant-apache-oro</artifactId>
                                                <version>1.9.3</version>
                                </dependency>
                                                <dependency>
                                                <groupId>commons-discovery</groupId>
                                                <artifactId>commons-discovery</artifactId>
                                                <version>0.4</version>
                                                <scope>compile</scope>
                                </dependency>
                                <dependency>
                                                <groupId>com.google.code.gson</groupId>
                                                <artifactId>gson</artifactId>
                                                <version>2.2.4</version>
                                </dependency>

                                <dependency>
                                                <groupId>junit</groupId>
                                                <artifactId>junit</artifactId>
                                                <version>4.11</version>
                                                <scope>test</scope>
                                </dependency>
                                <dependency>
                                                <groupId>commons-codec-1.3</groupId>
                                                <artifactId>commons-codec-1.3.jar</artifactId>
                                                <version>1.0</version>
                                                <scope>system</scope>
                                                <systemPath>${project.libdir}/commons-codec-1.3.jar</systemPath>
                                </dependency>


                                <dependency>
                                                <groupId>org.springframework</groupId>
                                                <artifactId>spring-aop</artifactId>
                                                <version>${spring.version}</version>
                                </dependency>
                                <dependency>
                                                <groupId>org.springframework</groupId>
                                                <artifactId>spring-core</artifactId>
                                                <version>${spring.version}</version>
                                </dependency>
                                <dependency>
                                                <groupId>org.springframework</groupId>
                                                <artifactId>spring-context</artifactId>
                                                <version>${spring.version}</version>
                                </dependency>
                                <dependency>
                                                <groupId>org.springframework</groupId>
                                                <artifactId>spring-context-support</artifactId>
                                                <version>${spring.version}</version>
                                </dependency>
                                <dependency>
                                                <groupId>org.springframework</groupId>
                                                <artifactId>spring-web</artifactId>
                                                <version>${spring.version}</version>
                                </dependency>


                                <dependency>
                                                <groupId>org.springframework</groupId>
                                                <artifactId>spring-test</artifactId>
                                                <version>${spring.version}</version>
                                </dependency>



                                <dependency>
                                                <groupId>org.springframework.integration</groupId>
                                                <artifactId>spring-integration-core</artifactId>
                                                <version>2.0.3.RELEASE</version>
                                                <scope>test</scope>
                                </dependency>

                                <dependency>
                <groupId>org.springframework.integration</groupId>
                <artifactId>spring-integration-mail</artifactId>
                <version>2.0.3.RELEASE</version>
                <scope>test</scope>
</dependency>


    <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>1.6.7</version>
    </dependency>

 <dependency>
                                                <groupId>org.aspectj</groupId>
                                                <artifactId>aspectjweaver</artifactId>
                                                <version>1.8.4</version>
                                                <scope>system</scope>
                                                <systemPath>${project.libdir}/aspectjweaver-1.8.4.jar</systemPath>
                                </dependency>

                                <dependency>
                                                <groupId>org.springagent</groupId>
                                                <artifactId>spring-agent</artifactId>
                                                <version>2.5.6</version>
                                                <scope>system</scope>
                                                <systemPath>${project.libdir}/spring-agent-2.5.6.jar</systemPath>
                                </dependency>

                </dependencies>
                <profiles>
                                <profile>
                                                <id>local</id>
                                                <activation>
                                                                <activeByDefault>true</activeByDefault>
                                                </activation>
                                                <properties>
                                                                <env>local</env>
                                                </properties>
                                </profile>
                                <profile>
                                                <id>dev</id>
                                                <properties>
                                                                <env>dev</env>
                                                </properties>
                                </profile>
                                <profile>
                                                <id>stage</id>
                                                <properties>
                                                                <env>stage</env>
                                                </properties>
                                </profile>
                                <profile>
                                                <id>lt</id>
                                                <properties>
                                                                <env>lt</env>
                                                </properties>
                                </profile>
                                <profile>
                                                <id>prod</id>
                                                <properties>
                                                                <env>prod</env>
                                                </properties>
                                </profile>

                </profiles>
                <build>



                               <plugins>
                                                <plugin>
                                                                <artifactId>maven-resources-plugin</artifactId>
                                                                <version>2.6</version>
                                                                <executions>
                                                                                <execution>
                                                                                                <id>generate-resources</id>
                                                                                                <phase>process-sources</phase>
                                                                                                <goals>
                                                                                                                <goal>copy-resources</goal>
                                                                                                </goals>
                                                                                                <configuration>
                                                                                                                <overwrite>true</overwrite>
                                                                                                                <outputDirectory>${project.build.directory}/classes/config</outputDirectory>
                                                                                                                <resources>
                                                                                                                                <resource>
                                                                                                                                                <directory>${project.basedir}/src/main/resources/config/${env}</directory>
                                                                                                                                                <includes>
                                                                                                                                                                <include>**/*.properties</include>
                                                                                                                                                </includes>
                                                                                                                                                <excludes>
                                                                                                                                                                <exclude>${project.basedir}/src/main/resources/config/${env}</exclude>
                                                                                                                                                </excludes>
                                                                                                                                </resource>
                                                                                                                </resources>
                                                                                                </configuration>
                                                                                </execution>
                                                                </executions>
                                                </plugin>
                                                <plugin>
                                                                <groupId>org.apache.maven.plugins</groupId>
                                                                <artifactId>maven-compiler-plugin</artifactId>
                                                                <version>2.0.2</version>
                                                                <configuration>
                                                                                <source>1.6</source>
                                                                                <target>1.6</target>
                                                                </configuration>
                                                </plugin>
                                                <plugin>
                                                                <groupId>org.apache.maven.plugins</groupId>
                                                                <artifactId>maven-war-plugin</artifactId>
                                                                <version>2.4</version>
                                                                <configuration>
                                                                                <warName>${project.artifactId}-${project.version}</warName>
                                                                                <webResources>
                                                                                                <resource>
                                                                                                                <directory>${project.libdir}</directory>
                                                                                                                <targetPath>WEB-INF/lib</targetPath>
                                                                                                                <includes>
                                                                                                                                <include>**/*</include>
                                                                                                                </includes>
                                                                                                </resource>
                                                                                </webResources>
                                                                </configuration>
                                                </plugin>
                                                <plugin>
                                                                <groupId>org.apache.maven.plugins</groupId>
                                                                <artifactId>maven-ear-plugin</artifactId>
                                                                <version>2.9</version>
                                                                <configuration>
                                                                                <skinnyWars>true</skinnyWars>
                                                                                <modules>
                                                                                                <webModule>
                                                                                                                <groupId>${project.groupId}</groupId>
                                                                                                                <artifactId>project-Web</artifactId>
                                                                                                                <contextRoot>/dev</contextRoot>
                                                                                                </webModule>
                                                                                </modules>
                                                                </configuration>
                                                                <dependencies>
                                                                                <dependency>
                                                                                                <groupId>comprojectservices</groupId>
                                                                                                <artifactId>project-Web</artifactId>
                                                                                                <version>0.0.1-SNAPSHOT</version>
                                                                                                <type>war</type>
                                                                                                <scope>system</scope>
                                                                                                <systemPath>${project.basedir}/target/project-Web-0.0.1-SNAPSHOT.war</systemPath>
                                                                                </dependency>
                                                                </dependencies>
                                                </plugin>
            <plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>aspectj-maven-plugin</artifactId>
     <version>1.3</version>
     <executions>
         <execution>
             <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>     

             <goals>
                 <goal>compile</goal> 
                 <goal>test-compile</goal>    
             </goals>
         </execution>
    </executions>
</plugin>
            <plugin>
    <groupId>org.apache.maven.plugins</groupId>


   <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4</version>
    <configuration>
            <forkMode>once</forkMode>
            <argLine>
            -javaagent:${settings.localRepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar -javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/1.8.4/aspectjweaver-1.8.4.jar
        </argLine>
            <useSystemClassloader>true</useSystemClassloader>
    </configuration>
</plugin>
                                </plugins>
                                <resources>
      <resource>
        <directory>test/resources</directory>
        <includes>
          <include>aop.xml</include>
        </includes>
        <targetPath>META-INF</targetPath>
      </resource>
    </resources>
                </build>
</project>


    still aop.xml is not getting picked up, becase aspects are not getting weaved.
    i tried all approaches but not getting any clue where i am going wrong.
    **BTW :** both the javaagents are in one line only.
    does maven-resource-plugin is causing any issue here?

2 个答案:

答案 0 :(得分:1)

如果您只想打包已经拥有的自定义aop.xml(而不是通过aspectj-maven-plugin生成META-INF),您只需要调整<resources>配置的<build>部分如下:

  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>aop.xml</include> <!-- ensures aop.xml ends inside the META-INF -->
        </includes>
        <targetPath>META-INF</targetPath>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <excludes>
          <exclude>aop.xml</exclude> <!-- ensures all other resources end inside the default target path -->
        </excludes>
      </resource>
    </resources>
    ...
  </build>

这假设您已将aop.xml文件放在src/main/resources下 - 您可以根据需要对其进行配置。

答案 1 :(得分:0)

以下是让aspectj-maven-plugin生成aop.xml下的META-INF以后load-time-weaving用于Maven所需的最低配置。请务必在项目中包含aspectjrt依赖项。

您可以在任何项目上尝试此操作 - 即使是准系统META-INF\aop.xml项目也应足以测试此项并验证是否在您的存档中创建了 <build> <plugins> ... <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <version>1.7</version> <configuration> <outxml>true</outxml> <outxmlfile>META-INF/aop.xml</outxmlfile> <source>1.6</source> <target>1.6</target> <complianceLevel>1.6</complianceLevel> </configuration> <executions> <execution> <id>Generate aop.xml for LTW</id> <goals> <goal>compile</goal> </goals> <phase>compile</phase> </execution> </executions> </plugin> </plugins> </build> 。当然,您可以调整配置以满足您的需求。

{{1}}