AspectJ プロジェクトをMavenでビルドする

Google先生に聞いて解説ブログを見つけたにも関わらず、設定に躓いたので自分用の備忘録として残しておく。

コード

前回の記事で書いたpropwarrperのテストを作成し、Mavenでビルドしてみました。

フォルダ構成
■propwarrper
 pom.xml
 ■src
  ■main
   ■java
    App.java
   ■aspectj
    propwarrper.aj
  ■test
   ■java
    AppTest.java

Mavenの標準構成にaspectjのソースフォルダを追加しています。

AppTest.java
package sinsoku.propwrapper;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        App.main(new String[1]);
        assertTrue( true );
    }
}

目視確認用なので、テストとしては酷いが、まぁ勉強用なのでw

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>sinsoku.propwrapper</groupId>
  <artifactId>propwrapper</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>propwrapper</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.0</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.6.5</version></dependency>
  </dependencies>
  <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <complianceLevel>1.5</complianceLevel>
                </configuration>
            </plugin>
            
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/aspectj</source>
                        </sources>
                   </configuration>
               </execution>
           </executions>
        </plugin>
                  
        </plugins>
    </build>
</project>
出力結果
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building propwrapper
[INFO]    task-segment: [clean, test]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory C:\Tools\Eclipse_AOP\workspace\PropWrapper\target
[INFO] [build-helper:add-source {execution: add-source}]
[INFO] Source directory: C:\Tools\Eclipse_AOP\workspace\PropWrapper\src\main\aspectj added.
[INFO] [aspectj:compile {execution: default}]
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (MS932 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [aspectj:test-compile {execution: default}]
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (MS932 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Tools\Eclipse_AOP\workspace\PropWrapper\src\test\resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: C:\Tools\Eclipse_AOP\workspace\PropWrapper\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running sinsoku.propwrapper.AppTest
はろーわーるど
Hello World
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.032 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7 seconds
[INFO] Finished at: Mon Nov 16 21:31:46 JST 2009
[INFO] Final Memory: 11M/20M
[INFO] ------------------------------------------------------------------------

今回のフォルダ構成のようにアスペクトとソースのフォルダを分けている場合、build-helper-maven-pluginを追加する必要があります。
追加し忘れると、Eclipse(+AJDT)ではアスペクトが織り込まれるのに、Mavenでは織り込まれないことになります。

・・・ここで1時間くらい躓きましたorz