Weave your Application

Now use you aspect on your application

In the actual version of AspectJ, the code instrumentation need to be done at compile time.

Code instrumentation

You have different solutions for doing it:

We suppose that your application is located in one (ore more) module and your aspects in another one.
This organisation is use in our sample, your can find 3 modules:

  • sample_aspects: contains only the aspects used with jmonitoring
  • sample_core: contains the code your want to monitore
  • sample_webapp: packaging of the application for the execution as a web application

Sommaire

How to weave your application with Maven 2

How to weave your application with Maven 1

How to weave your application with ANT

How to weave your application with Maven 2

You have an exemple project with the jmonitoring sample : http://forge.octo.com/svn/jmonitoring/trunk/sample/sample-web and the documentation Maven 2 AspectJ plugin.

How to weave your application with Maven 1

sample_aspects

Declare the location of the aspects and dependencies:

project.xml
<build>
    <aspectSourceDirectory>src-aspectj</aspectSourceDirectory>
</build>

</dependencies>
    <dependency>
        <groupId>jmonitoring</groupId>
        <artifactId>core_monitoring</artifactId>
        <version>${monitoring.version}</version>
        <url>http://octo.dnalias.com/maven</url>
        <properties>
            <aspectj.weaveWith>true</aspectj.weaveWith>
        </properties>
    </dependency>
    <dependency>
        <groupId>aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.2.1</version>
        <url>http://eclipse.org/aspectj/</url>
    </dependency>
    <dependency>
         <groupId>aspectj</groupId>
         <artifactId>aspectj</artifactId>
         <version>1.2.1</version>
         <url>http://eclipse.org/aspectj/</url>
    </dependency>
</dependencies>

Declare the compilation of the aspects:

maven.xml
<preGoal name="java:compile">
    <attainGoal name="aspectj"/>
</preGoal>

sample_core

Declare the location of the aspects (even you don't have any aspect in this module) and dependencies:

project.xml
<build>
    <aspectSourceDirectory>src-aspectj</aspectSourceDirectory>
</build>
<dependencies>
     <dependency>
         <groupId>jmonitoring</groupId>
         <artifactId>core_monitoring</artifactId>
         <version>${monitoring.version}</version>
         <url>http://octo.dnalias.com/maven</url>
         <properties>
             <aspectj.weaveWith>true</aspectj.weaveWith>
         </properties>
     </dependency>

     <dependency>
         <groupId>jmonitoring</groupId>
         <artifactId>sample_aspects</artifactId>
         <version>${monitoring.version}</version>
         <url>http://octo.dnalias.com/maven</url>
         <properties>
             <aspectj.weaveWith>true</aspectj.weaveWith>
         </properties>
    </dependency>
    <dependency>
        <groupId>aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.2.1</version>
        <url>http://eclipse.org/aspectj/</url>
    </dependency>

    <dependency>
        <groupId>aspectj</groupId>
        <artifactId>aspectj</artifactId>
        <version>1.2.1</version>
        <url>http://eclipse.org/aspectj/</url>
    </dependency>
</dependencies>

Declare the compilation of the aspects:

maven.xml
<preGoal name="java:compile">
    <attainGoal name="aspectj"/>
</preGoal>

sample_webapp

Add runtime dependencies:

project.xml
<dependencies>
    <dependency>
        <groupId>jmonitoring</groupId>
        <artifactId>core_monitoring</artifactId>
        <version>${monitoring.version}</version>
        <url>http://octo.dnalias.com/maven</url>
        <properties>
            <eclipse.dependency>true</eclipse.dependency>
            <war.bundle>true</war.bundle>
        </properties>
    </dependency>
    <dependency>
        <groupId>jmonitoring</groupId>
        <artifactId>sample_core</artifactId>
        <version>${monitoring.version}</version>
        <url>http://octo.dnalias.com/maven</url>
        <properties>
            <eclipse.dependency>true</eclipse.dependency>
            <war.bundle>true</war.bundle>
        </properties>
    </dependency>

    <dependency>
        <groupId>jmonitoring</groupId>
        <artifactId>sample_aspects</artifactId>
        <version>${monitoring.version}</version>
        <url>http://octo.dnalias.com/maven</url>
        <properties>
            <eclipse.dependency>true</eclipse.dependency>
            <war.bundle>true</war.bundle>
        </properties>
    </dependency>

    <dependency>
        <id>concurrent</id>
        <version>1.3.4</version>
        <url>http://gee.cs.oswego.edu/dl//</url>
        <properties>
            <eclipse.dependency>true</eclipse.dependency>
            <war.bundle>true</war.bundle>
        </properties>
    </dependency>

    <dependency>
        <id>log4j</id>
        <version>1.2.8</version>
        <url>http://www.log4j.org/</url>
        <properties>
            <war.bundle>true</war.bundle>
        </properties>
    </dependency>
        
    <dependency>
        <id>commons-logging</id>
        <version>1.0.4</version>
        <url>http://www.apache.org/</url>
        <properties>
            <war.bundle>true</war.bundle>
        </properties>
    </dependency>

    <dependency>
        <id>commons-digester</id>
        <version>1.6</version>
        <url>http://www.apache.org/</url>
        <properties>
            <war.bundle>true</war.bundle>
        </properties>
    </dependency>

    <dependency>
        <id>commons-beanutils</id>
        <version>1.7.0</version>
        <url>http://www.apache.org/</url>
        <properties>
            <war.bundle>true</war.bundle>
        </properties>
    </dependency>

    <dependency>
        <id>commons-fileupload</id>
        <version>1.0</version>
        <url>http://www.apache.org/</url>
        <properties>
            <war.bundle>true</war.bundle>
        </properties>
    </dependency>

    <dependency>
        <id>commons-validator</id>
        <version>1.1.4</version>
        <url>http://www.apache.org/</url>
        <properties>
            <war.bundle>true</war.bundle>
        </properties>
    </dependency>

    <dependency>
        <groupId>aspectj</groupId>
         <artifactId>aspectjrt</artifactId>
         <version>1.2.1</version>
         <url>http://eclipse.org/aspectj/</url>
         <properties>
             <war.bundle>true</war.bundle>
         </properties>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>3.1.11-bin</version>
        <url>http://www.mysql.com/</url>
        <properties>
            <war.bundle>true</war.bundle>
        </properties>
    </dependency>
</dependencies>

How to weave your application with ANT

Add taskdef definition:

build.xml
  <taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties">
    <classpath>
      <pathelement location="${libdir}/aspectj/jars/aspectjtools-1.5.0.jar"/>
    </classpath>  
  </taskdef>

Use the iajc compiler instead of Javac:

build.xml
  <target name="compile" description="o Compile the code">
    <mkdir dir="${classesdir}"/>
    <iajc destDir="${classesdir}" deprecation="true" debug="true" excludes="**/package.html">
      <sourceroots>
        <pathelement location="${basedir}/src-java"/>
      </sourceroots>
      <aspectpath>
        <pathelement location="${libdir}/jmonitoring/jars/sample_aspects-1.0-SNAPSHOT.jar"/>
      </aspectpath>
      <classpath refid="build.classpath"/>
    </iajc>
    <copy todir="${classesdir}">
      <fileset dir="${basedir}/src-java">
        <include name="**/*.xml"/>
        <include name="**/*.properties"/>
        <include name="**/*.java"/>
        <exclude name="**/.svn"/>
      </fileset>
    </copy>
    <copy todir="${classesdir}/META-INF" file="${basedir}/LICENSE.txt"/>
  </target>

Then add the runtime dependencies to your runtime classpath:

<pathelement location="${libdir}/jmonitoring/jars/core_monitoring-${monitoring.version}.jar"/>
<pathelement location="${libdir}/jmonitoring/jars/sample_core-${monitoring.version}.jar"/>
<pathelement location="${libdir}/jmonitoring/jars/sample_aspects-${monitoring.version}.jar"/>
<pathelement location="${libdir}/concurrent/jars/concurrent-1.3.4.jar"/>
<pathelement location="${libdir}/log4j/jars/log4j-1.2.8"/>
<pathelement location="${libdir}/commons-logging/jars/commons-logging-1.0.4.jar"/>
<pathelement location="${libdir}/commons-digester/jars/commons-digester-1.6.jar"/>
<pathelement location="${libdir}/commons-beanutils/jars/commons-digester-1.7.0.jar"/>
<pathelement location="${libdir}/commons-fileupload/jars/commons-fileupload-1.0.jar"/>
<pathelement location="${libdir}/commons-validator/jars/commons-validator-1.1.4.jar"/>
<pathelement location="${libdir}/aspectj/jars/aspectjrt-1.2.1.jar"/>
<pathelement location="${libdir}/mysql/jars/mysql-connector-java-3.1.11-bin.jar"/>
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.