Spring Boot打jar包,排除lombok等scope=provided的依赖(转)

lombok可以使实体更简洁,主要是会在编译.class文件期间,生成get/set方法,还有hash/equals/toString等。

主要是可以让代码更简洁。

@Data//生成get/set/toString/hash/equals等
@Accessors(chain = true)//开启链式调用,即H2user user = new H2user().setId(1).setName("https://www.cnblogs.com/tomcatandjerry/").setAge(88);
public class H2user {

    private Long id;
    private String name;
    private Integer age;

}

在maven项目的pom依赖:

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.16.18</version>
  <scope>provided</scope>
</dependency>

Maven scope=provided的作用范围:参考:http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

    • provided
      This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

所以像lombok这样的依赖只作用于编译阶段,编译完成就没用了,可以从spring-boot的jar包里排除掉。

默认Spring Boot项目的pom配置:

<plugins>
     <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
     </plugin>
</plugins>

但是执行mvn package,你会发现lombok这样的provided的依赖还是进了jar(your-spring-boot.jar\BOOT-INF\lib\)

这会增加最终的jar包的体积(当然有一些代码洁癖的人也是不能忍的)

 

解决方案:

spring-boot-maven-plugin官方目前给出的解决方案是:

<plugins>
     <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <configuration>
             <excludes>
                 <exclude>
                     <groupId>org.projectlombok</groupId>
                     <artifactId>lombok</artifactId>
                 </exclude>
             </excludes>
         </configuration>
     </plugin>
 </plugins>

配置后,重新执行:mvn clean package, lombok的jar已经不在boot.jar里面,应用也可以正常启动: mvn spring-boot:run  或者 java -jar boot.jar

 

参考:https://www.cnblogs.com/tomcatandjerry/p/10342345.html

Spring Boot打jar包,排除lombok等scope=provided的依赖(转)

上一篇:Android SELinux的avc: denied log是哪里打印的及关闭


下一篇:iOS内存布局