gradle build不会单独编译出内部类而boot jar里面的class内部类是单独的文件
时间: 2024-04-29 07:21:37 浏览: 10
是的,这是因为 Gradle 在编译 Java 代码时,默认会将内部类(包括匿名内部类)编译为与外部类在同一个 .class 文件中。这种做法可以减少 .class 文件的数量,但也会对代码的可读性和维护性造成一定的影响。
而在生成 boot jar 时,Spring Boot 会使用 Spring Boot Plugin 提供的 BootRepackage 任务来重新打包应用,该任务会将所有的 .class 文件重新打包为一个 jar 文件,并且会将内部类编译为单独的 .class 文件。这样做的目的是为了确保应用在运行时能够正确加载所有的 .class 文件,从而避免出现类加载异常等问题。
因此,在使用 Gradle 和 Spring Boot 开发应用时,需要注意这种编译和打包方式的差异,以避免出现不必要的问题。
相关问题
gradle build不会单独编译出内部类而gradle bootJar里面的class内部类是单独的文件
是的,这是因为 Gradle 在编译 Java 代码时,默认会将内部类(包括匿名内部类)编译为与外部类在同一个 .class 文件中,而在生成 bootJar 时,Spring Boot 会使用 Spring Boot Plugin 提供的 bootJar 任务来重新打包应用,该任务会将所有的 .class 文件重新打包为一个 jar 文件,并且会将内部类编译为单独的 .class 文件,即使在使用 Gradle 进行编译时,内部类也会被单独编译出来。
这样做的目的是为了确保应用在运行时能够正确加载所有的 .class 文件,从而避免出现类加载异常等问题。因此,在使用 Gradle 和 Spring Boot 开发应用时,需要注意这种编译和打包方式的差异,以避免出现不必要的问题。
sping boot 如何将外部引入的jar包打到fat jar里面
如果你使用 Maven 或 Gradle 构建你的 Spring Boot 项目,可以将外部引入的 jar 包打包到 fat jar 里面。下面是两个常用的方法:
### Maven
在 pom.xml 文件中,使用 `maven-shade-plugin` 插件可以将所有依赖打包为一个可执行的 fat jar。在插件配置中,使用 `include` 指定需要打包的 jar 包,使用 `mainClass` 指定启动类。
```xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<finalName>${project.artifactId}-${project.version}</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.example.Application</Main-Class>
</manifestEntries>
</transformer>
</transformers>
<artifactSet>
<includes>
<include>com.example:external-lib:jar:1.0.0</include>
</includes>
</artifactSet>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
```
### Gradle
在 build.gradle 文件中,使用 `shadow` 插件可以将所有依赖打包为一个可执行的 fat jar。在插件配置中,使用 `include` 指定需要打包的 jar 包,使用 `mainClassName` 指定启动类。
```groovy
plugins {
id 'com.github.johnrengelman.shadow' version '7.0.0'
}
shadowJar {
exclude 'META-INF/*.SF'
exclude 'META-INF/*.DSA'
exclude 'META-INF/*.RSA'
baseName = "${rootProject.name}-${project.name}"
classifier = null
mainClassName = 'com.example.Application'
mergeServiceFiles {
include 'META-INF/services/*'
}
configurations = [project.configurations.compile]
}
dependencies {
implementation 'com.example:external-lib:1.0.0'
}
```
以上是两个常用的方法,可以根据自己的需要进行选择。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)