0

Maven is not able to package the aop.xml file when we are using spring-agent.jar file.

We are using Surefire for running unit test cases and jacoco for getting the code coverage.

We are not seeing any issues with spring-agent but aspects are not getting called during run time. We have placed the aop.xml under META-INF folder but it is not getting picked up.

Any idea or pointers on how to make Maven pick up aop.xml file and package it inside the META-INF folder?

this is my pom.xml file

*edit**

this is my pom.xml.. some stuff i removed

                <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?
shan
  • 101
  • 10
  • "_Maven is not able to pick up aop.xml file_" - you mean Maven is not packaging the `aop.xml` within the archive (`jar`/`war`/`ear` or any such) despite putting the file in the right place in your project? – mystarrocks Jan 09 '15 at 12:41
  • Yes u got it correct. – shan Jan 09 '15 at 13:57
  • Just create a folder named "META-INF" under /src/main/resources and place the aop.xml there - works for beans.xml, persistence.xml and any other file, which has to be placed under META-INF – gclaussn Jan 12 '15 at 13:35

2 Answers2

1

If you just want to package a custom aop.xml that you already have with you (as opposed to generating it through the aspectj-maven-plugin) inside the META-INF, you would only need to tweak the <resources> section of the <build> configuration like this:

  <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>

This assumes you've placed your aop.xml file under src/main/resources - you can configure this according to your needs.

mystarrocks
  • 4,040
  • 2
  • 36
  • 61
  • Is there a way i can verify that the current project setup is properly set as per maven standards. Why i am asking this question is because i created a maven project in eclipse and placed aop.xml in META-INF folder and it is getting picked up without any issues and all aspects are applied. but the same is not happening is another project. structurally both seems to be the same but in one project aop.xml is not read n in another it is read properly. both are part of the same workspace in eclipse. i am not sure where the issue is – shan Jan 30 '15 at 17:24
0

Here's the minimal configuration you will need to let the aspectj-maven-plugin generate the aop.xml under META-INF to be used for load-time-weaving later. Be sure to include the aspectjrt dependency to your project too.

You can try this on any project - even a barebone Maven project should be enough to test this and verify that the META-INF\aop.xml is created inside your archive. Of course you can tweak the configuration to suit your needs.

  <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>
mystarrocks
  • 4,040
  • 2
  • 36
  • 61
  • thanks a lot. But i think META-INF/aop.xml will generate anew aop.xml or will it consider the one we are having. we want maven to pick up our custom aop.xml and not to generate a new one of its own. – shan Jan 09 '15 at 17:04
  • This will generate an `aop.xml` and package it inside `META-INF`. If you just want to package a custom `aop.xml` inside the `META-INF`, you would need to tweak the `` configuration accordingly. I'll add another answer for this. – mystarrocks Jan 09 '15 at 18:34
  • I have posted my pom.xml file and i also added the resources stuff which was mentioned by mystarrocks.. but it seems i am doing sth wrong.. still my classes are not getting weaved – shan Jan 15 '15 at 15:37
  • Not getting woven? You didn't want your aspects woven into the classes at `compile-time`, did you? This solution is for `load-time-weaving` and the only check you have to make is the `aop.xml` is packaged inside the `META-INF`. Did you instead mean to say it's not getting packaged inside the `META-INF` folder? – mystarrocks Jan 15 '15 at 17:35
  • > we want LTW only and as seen we have defined weave option for com.company.services.stuff..* but it is not working..as the actual methods are getting called and not the mocked ones which we defined in mockadvisor class. the same code is working properly on eclipse and we are seeing mocking happening properly. so not sure where we are going wrong. we are also not seeing any debug information as we have enabled it also. so not sure where we are wrong – shan Jan 16 '15 at 09:46
  • OK. But this question was about *`Maven`* not doing its job of packaging the `aop.xml` inside the right folder in the archive. Is that issue resolved? And if so, it's rather a case of `LTW` not happening despite packaging the `aop.xml` correctly? If yes, you'll want to start a new question that does not even mention `Maven` in it. – mystarrocks Jan 16 '15 at 12:35
  • This is the final observation i had. when i am running maven with -X i am able to see DEBUG messages with classes getting woven but the same is not getting picked up when test cases are run. so i think that aop.xml is picked up but the issue of why the actual classes are used and not the woven one is not clear to me. is there anything wrong in the above pom.xml file? – shan Jan 17 '15 at 07:15