maven使用

_:nil

TOC TOC

maven中同时使用aspectj和lombok

参考:https://palesz.wordpress.com/2011/12/03/howto-maven-lombok-and-aspectj-together/

keypoint 是让 aspectj 更改class文件,而不是更改java代码。

最终要添加的配置是:

<forceAjcCompile>true</forceAjcCompile>
<sources/>
<weaveDirectories>
    <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
</weaveDirectories>

完整的plugin设置阔以参考:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
      <!--                    <annotationProcessors>-->
      <!--                        <annotationProcessor>-->
      <!--                            org.mapstruct.ap.MappingProcessor-->
      <!--                        </annotationProcessor>-->
      <!--                    </annotationProcessors>-->
      <annotationProcessorPaths>
        <path>
          <groupId>org.mapstruct</groupId>
          <artifactId>mapstruct-processor</artifactId>
          <version>${org.mapstruct.version}</version>
        </path>
        <path>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.18.10</version>
        </path>
      </annotationProcessorPaths>
    </configuration>
  </plugin>
  <!--            <plugin>-->
  <!--                <groupId>org.projectlombok</groupId>-->
  <!--                <artifactId>lombok-maven-plugin</artifactId>-->
  <!--                <version>1.18.10.0</version>-->
  <!--                <executions>-->
  <!--                    <execution>-->
  <!--                        <phase>generate-sources</phase>-->
  <!--                        <goals>-->
  <!--                            <goal>delombok</goal>-->
  <!--                        </goals>-->
  <!--                        <configuration>-->
  <!--                            <addOutputDirectory>false</addOutputDirectory>-->
  <!--                            <sourceDirectory>src/main/java</sourceDirectory>-->
  <!--                        </configuration>-->
  <!--                    </execution>-->
  <!--                </executions>-->
  <!--            </plugin>-->
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.7</version>

    <configuration>
      <complianceLevel>1.8</complianceLevel>
      <source>1.8</source>
      <target>1.8</target>
      <outxml>true</outxml>
      <verbose>true</verbose>
      <showWeaveInfo>true</showWeaveInfo>
      <forceAjcCompile>true</forceAjcCompile>
      <sources/>
      <weaveDirectories>
        <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
      </weaveDirectories>
      <aspectLibraries>
        <aspectLibrary>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aspects</artifactId>
        </aspectLibrary>
      </aspectLibraries>
    </configuration>

    <executions>
      <execution>
        <phase>process-classes</phase>
        <goals>
          <goal>compile</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

有compiler插件是因为同时还有别的注解(mapstruct)需要处理。

不过还是有一些问题,如

maven shade jar 参考

这时候最好单独起项目,如果作为模块包含在别的项目里面,最终打包可能是成功的,但会导致idea等IDE找不到符号。

单独建立项目时,使用一个 pom.xml 即可,相关内容可以参考下面的实例(shaded guava和asynchttpclient)

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>pictureprocess</groupId>
    <artifactId>pictureprocess-deps-wrapper</artifactId>
    <version>1.0.1</version>

    <properties>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.asynchttpclient</groupId>
            <artifactId>async-http-client</artifactId>
            <version>2.10.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>netty-handler</artifactId>
                    <groupId>io.netty</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-handler</artifactId>
            <version>4.1.36.Final</version>
        </dependency>
        <dependency>
            <groupId>com.google.truth</groupId>
            <artifactId>truth</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <relocations>
                                <relocation>
                                    <pattern>io.netty</pattern>
                                    <shadedPattern>shaded.io.netty</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>org.slf4j</pattern>
                                    <shadedPattern>shaded.org.slf4j</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>org.reactivestreams</pattern>
                                    <shadedPattern>shaded.org.reactivestreams</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>com.google</pattern>
                                    <shadedPattern>shaded.com.google</shadedPattern>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.google.errorprone</groupId>
                <artifactId>error_prone_annotations</artifactId>
                <version>2.3.1</version>
            </dependency>
            <dependency>
                <groupId>org.checkerframework</groupId>
                <artifactId>checker-compat-qual</artifactId>
                <version>2.5.5</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>