Search code examples
javaaopaspect

How to disable AspectJ compilation via configuration


I implement my aspect class in Java style:

@Aspect
public class SomeAspect {
  @Pointcut("...")
  public void somePointcut() {}

  @Around("somePointcut()")
  public ...
}

I am new to AspectJ. I want to know if there a convenient/centralized way to control the effectiveness of the Pointcut so I can enable/disable them based on some configuration at compile time.


Solution

  • During runtime you can just use an if() pointcut, but I know this is not what you want.

    For compile time it depends on how you build your project. I am going to assume you use Maven and the AspectJ Maven Plugin.

    Let us further assume that you configure the plugin to run in the process-sources phase like this:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.8</version>
        <configuration>
            <!--<showWeaveInfo>true</showWeaveInfo>-->
            <source>${java.source-target.version}</source>
            <target>${java.source-target.version}</target>
            <Xlint>ignore</Xlint>
            <complianceLevel>${java.source-target.version}</complianceLevel>
            <encoding>${project.build.sourceEncoding}</encoding>
            <!--<verbose>true</verbose>-->
            <!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn>-->
        </configuration>
        <executions>
            <execution>
                <!-- IMPORTANT -->
                <phase>process-sources</phase>
                <goals>
                    <goal>compile</goal>
                    <goal>test-compile</goal>
                </goals>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjtools</artifactId>
                <version>${aspectj.version}</version>
            </dependency>
        </dependencies>
    </plugin>
    

    Then just add a Maven profile - let us call it skip-aspectj which overwrites the phase to none via <phase/>:

    <profiles>
        <profile>
            <id>skip-aspectj</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>aspectj-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase/>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    

    Now if you run your Maven build

    • normally, the aspects are compiled and applied,
    • with mvn -P skip-aspectj ... they will be inactive and the whole AspectJ Maven execution just skipped.