Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

java - How to build aspectj project using maven?

I have created a Aspectj Project in Eclipse ide but i need to build it using maven.

I have maven-aspectj plugin but don't know how to use it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Below are the steps that I followed to get this working. This gave me compile-time weaving. If you need other strategies, clearly you need another approach (such as Spring AOP for runtime AOP proxies).

  1. Add a property to standardize the AspectJ version that you use:

    <properties>
        <aspectj.version>1.7.2</aspectj.version>
      ...
    
  2. Add the runtime dependency:

    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>${aspectj.version}</version>
    </dependency>
    
  3. Add the AspectJ Maven plugin:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.4</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>test-compile</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
          </dependency>
          <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
          </dependency>
        </dependencies>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <forceAjcCompile>true</forceAjcCompile>
        </configuration>
      </plugin>
    

I'm not sure if forceAjcCompile makes a lot of sense, really, but I've seen some cases where the aspects weren't consistently applied. I'm blaming this (for now) on Eclipse overwriting class files or something, hence the forceAjcCompile.

Additional things I did:

  1. Add src/main/aspects as an extra source directory (build-helper-maven-plugin plugin). Just because it looks good in Eclipse
  2. Add a pluginExecution/pluginExecutionFilter for the AspectJ plugin (lifecycle-mapping plugin) and set it to execute and runOnIncremental, so that also aspects are (re-)applied when coding and testing in Eclipse (using m2e)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...