File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change
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
+ ```
You can’t perform that action at this time.
0 commit comments