AspectJ和Lombok工具的介绍:
AspectJ:Eclipse AspectJ是Java™编程语言的无缝的面向方面的扩展。 它与Java平台兼容,易于学习和使用。
Aspectj有三个包,分别是aspectjrt.jar, aspectjtools.jar和aspectjweaver.jar。其中aspectjweaver.jar内包含aspectjrt.jar的所有内容,他可以实现类加载器加载的时候织入代码。而当需要使用ajc编译器来编译时或者编译后织入代码的时候还需要引入aspectjrt.jar,aspectjtools.jar是一个eclipse插件,他就是ajc。
Lombok:Lombok项目是一个Java库,它会自动插入您的编辑器和构建工具中,从而为您的Java增光添彩。
永远不要再写另一个getter或equals方法,带有一个注释的您的类有一个功能全面的生成器,自动执行日志记录变量等等。
正文:
想要同时使用Aspectj的aspectjweaver.jar和lombok普通的情况下是完全ok的,但是如果想要使用aspectjtools.jar采用编译时织入的话将会造成失败。Lombok将会给出关键的警告信息:
You aren't using a compiler supported by lombok, so lombok will not work and has been disabled.
Your processor is: org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.BatchProcessingEnvImpl
Lombok supports: sun/apple javac 1.6, ECJ
我通过网络查找到可以使用lombok-maven-plugin插件配合aspectj-maven-plugin完成我们想要的功能。
参考链接:
https://stackoverflow.com/questions/25903686/how-to-make-lombok-and-aspectj-work-together
https://www.mojohaus.org/aspectj-maven-plugin/examples/includeExclude.html
思路是:先通过lombok-maven-plugin插件生成java文件,同时修改aspectj-maven-plugin所指定的源文件到lombok-maven-plugin生成的java文件目录就行了。
我的关键的maven代码如下:
<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/xsd/maven-4.0.0.xsd">
<properties>
<aspectj.version>1.8.13</aspectj.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<profiles>
<profile>
<id>default</id>
<properties>
<sourceDirectory>src/main/java</sourceDirectory>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<!-- <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.13</version>
</dependency>-->
</dependencies>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>ajc</id>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<properties>
<sourceDirectory>${project.build.directory}/generated-sources/delombok</sourceDirectory>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.12.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
</execution>
</executions>
<configuration>
<addOutputDirectory>false</addOutputDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<configuration>
<complianceLevel>${java.version}</complianceLevel>
<verbose>true</verbose>
<source>${java.version}</source>
<target>${java.version}</target>
<sources>
<source>
<basedir>${sourceDirectory}</basedir>
<includes>
<include>**/*.java</include>
</includes>
</source>
</sources>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<!--<configuration>
<skipTests>true</skipTests>
</configuration>-->
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
这样做的好处:
1、因为我默认使用的是profile id是default的内容,其中build里面的sourceDirectory指定的源文件是src/main/java是我们日常所需要的位置,并不是${project.build.directory}/generated-sources/delombok,避免了idea报错,因为idea会使用两个target目录,提示有重复的文件。
2、使用maven的profile,可以让我们随时切换,只需要在mvn命令后面增加-Pajc参数就行了。
3、当然是完成了我的目的(可以使用ajc编译时织入方式,我可以在生成的class文件中查看是否是我想要的效果)。
经过网友的告知,还有一种方式可以实现该功能,并且不需要使用lombok插件,并且不会造成ide显示警告,更加好用:
原文
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.10</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<proc>none</proc>
<complianceLevel>${java.version}</complianceLevel>
<showWeaveInfo>false</showWeaveInfo>
<forceAjcCompile>true</forceAjcCompile>
<sources/>
<weaveDirectories>
<weaveDirectory>${project.build.directory}/classes</weaveDirectory>
</weaveDirectories>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
和我的差异主要是对aspectj-maven-plugin插件的配置中<configuration>中<forceAjcCompile>中设置为true,并且设置<weaveDirectories>值设置为classes目录。为了网友能随时切换,我加上以上的profile代码如下:
<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/xsd/maven-4.0.0.xsd">
<properties>
<aspectj.version>1.8.13</aspectj.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<profiles>
<profile>
<id>default</id>
<properties>
<sourceDirectory>src/main/java</sourceDirectory>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<!-- <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.13</version>
</dependency>-->
</dependencies>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>ajc</id>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<build>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<configuration>
<complianceLevel>${java.version}</complianceLevel>
<verbose>true</verbose>
<source>${java.version}</source>
<target>${java.version}</target>
<showWeaveInfo>true</showWeaveInfo>
<forceAjcCompile>true</forceAjcCompile>
<weaveDirectories>
<weaveDirectory>
${project.build.directory}/classes
</weaveDirectory>
</weaveDirectories>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<!--<configuration>
<skipTests>true</skipTests>
</configuration>-->
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>