spring_reference/IX. ‘How-to’ guides/73.5. Create a non-executab...

66 lines
2.3 KiB
Markdown
Raw Normal View History

### 73.5. 使用排除创建不可执行的JAR
如果你构建的产物既有可执行的jar和非可执行的jar那你常常需要为可执行的版本添加额外的配置文件而这些文件在一个library jar中是不需要的。比如application.yml配置文件可能需要从非可执行的JAR中排除。
下面是如何在Maven中实现
```xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>exec</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<!-- Need this to ensure application.yml is excluded -->
<forceCreation>true</forceCreation>
<excludes>
<exclude>application.yml</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
```
在Gradle中你可以使用标准任务的DSL领域特定语言特性创建一个新的JAR存档然后在bootRepackage任务中使用withJarTask属性添加对它的依赖
```gradle
jar {
baseName = 'spring-boot-sample-profile'
version = '0.0.0'
excludes = ['**/application.yml']
}
task('execJar', type:Jar, dependsOn: 'jar') {
baseName = 'spring-boot-sample-profile'
version = '0.0.0'
classifier = 'exec'
from sourceSets.main.output
}
bootRepackage {
withJarTask = tasks['execJar']
}
```