Skip to content

Commit a9d257f

Browse files
committed
Update 73.5. Create a non-executable JAR with exclusions.md
1 parent 2a7fede commit a9d257f

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
### 73.5. 使用排除创建不可执行的JAR
2+
3+
如果你构建的产物既有可执行的jar和非可执行的jar,那你常常需要为可执行的版本添加额外的配置文件,而这些文件在一个library jar中是不需要的。比如,application.yml配置文件可能需要从非可执行的JAR中排除。
4+
5+
下面是如何在Maven中实现:
6+
```xml
7+
<build>
8+
<plugins>
9+
<plugin>
10+
<groupId>org.springframework.boot</groupId>
11+
<artifactId>spring-boot-maven-plugin</artifactId>
12+
<configuration>
13+
<classifier>exec</classifier>
14+
</configuration>
15+
</plugin>
16+
<plugin>
17+
<artifactId>maven-jar-plugin</artifactId>
18+
<executions>
19+
<execution>
20+
<id>exec</id>
21+
<phase>package</phase>
22+
<goals>
23+
<goal>jar</goal>
24+
</goals>
25+
<configuration>
26+
<classifier>exec</classifier>
27+
</configuration>
28+
</execution>
29+
<execution>
30+
<phase>package</phase>
31+
<goals>
32+
<goal>jar</goal>
33+
</goals>
34+
<configuration>
35+
<!-- Need this to ensure application.yml is excluded -->
36+
<forceCreation>true</forceCreation>
37+
<excludes>
38+
<exclude>application.yml</exclude>
39+
</excludes>
40+
</configuration>
41+
</execution>
42+
</executions>
43+
</plugin>
44+
</plugins>
45+
</build>
46+
```
47+
在Gradle中,你可以使用标准任务的DSL(领域特定语言)特性创建一个新的JAR存档,然后在bootRepackage任务中使用withJarTask属性添加对它的依赖:
48+
```gradle
49+
jar {
50+
baseName = 'spring-boot-sample-profile'
51+
version = '0.0.0'
52+
excludes = ['**/application.yml']
53+
}
54+
55+
task('execJar', type:Jar, dependsOn: 'jar') {
56+
baseName = 'spring-boot-sample-profile'
57+
version = '0.0.0'
58+
classifier = 'exec'
59+
from sourceSets.main.output
60+
}
61+
62+
bootRepackage {
63+
withJarTask = tasks['execJar']
64+
}
65+
```

0 commit comments

Comments
 (0)