aspectj-maven-plugin not weaving if sourceDirector

2019-07-18 13:12发布

问题:

Question

I have trouble weaving some aspects during compile time since I added the sourceDirectory tag with "src/main/java" as path. Before "." was set. Now the aspects are not weaved into the classes.

    ...
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>1.6.7</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.6.7</version>
    </dependency>
    ...
    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        ...
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            ...
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
        <encoding>utf-8</encoding>
        <complianceLevel>1.6</complianceLevel>                    
        <verbose>true</verbose>
            </configuration>
            </plugin>
            ...
      </plugins>
  </build>

It seems that the weaving gets skipped. If I set "." for sourceDirectory it will work again and aspectj will produce some output.

Solution

Oh man, it's friday afternoon...

For all running in same issue:

  • check that the directory of your aspects is "src/main/aspect" and not "src/main/aspects" or set the aspectDirectory parameter for plugin to know where your aspects are
  • check that if you used extension ".aj" for your aspects tell the plugin that he has to include all "*.aj" files
  • set the basedir where the plugin could find the java source

Sorry about that one here...

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.3</version>
            <executions>
                <execution>
                <phase>process-classes</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <sources>
                    <source>
                        <basedir>src/main/java</basedir>
                        <includes>
                            <include>**/*.aj</include>
                        </includes>
                    </source>
                </sources>              
                <source>1.6</source>
                <target>1.6</target>
                <encoding>utf-8</encoding>
                <complianceLevel>1.6</complianceLevel>                    
                <verbose>true</verbose>
                <showWeaveInfo>true</showWeaveInfo>
            </configuration>
        </plugin>