使用spring boot和spring-boot-maven-plugin生成war时排除application.properties

amg*_*han 9 java eclipse spring maven spring-boot

我正在使用Spring Boot开发一个Web应用程序,并希望生成war而不是jar.

使用从jar到war的转换,它可以很好地工作:http://spring.io/guides/gs/convert-jar-to-war/

但我想从战争中排除application.properties,因为我@PropertySource(value = "file:${OPENSHIFT_DATA_DIR}/application.properties")用来获取生产环境中的文件路径.

  • 这个方法在生成我的战争时有效,但在eclipse中我无法运行我的应用程序,因为application.properties根本没有复制到目标/类:

    <build>
        <resources> 
            <resource> 
                <directory>src/main/resources</directory> 
                <excludes> 
                    <exclude>application.properties</exclude> 
                </excludes> 
            </resource> 
         </resources> 
    </build>
    
    Run Code Online (Sandbox Code Playgroud)
  • 这个方法根本不起作用,我认为spring-boot-maven-plugin不支持packagingExcludes:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration> 
                    <packagingExcludes>WEB-INF/classes/application.properties</packagingExcludes> 
                </configuration> 
            </plugin>
        </plugins>
    </build>
    
    Run Code Online (Sandbox Code Playgroud)

你有另一个建议吗?

谢谢

nav*_*872 7

尝试使用下面的解决方案.这将有效:

<build>
    <resources>
         <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>**/*.properties</exclude>
            </excludes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

如果您使用上述解决方案,则在Eclipse IDE中运行项目时可能会收到找不到属性文件的错误.要摆脱这种情况,你需要在Run as configuration中添加资源文件夹.(运行配置... - > Classpath - > User Entries - > Advanced ... - > Add Folders)


amg*_*han 2

我添加的解决方案是解压我打包的war,删除文件application.properties并使用maven-antrun-plugin创建一个名为ROOT.war的新war并运行一些ant任务。

这是我在 pom.xml 中添加到我的插件中的内容:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
    <execution>
        <id>package</id>
        <phase>package</phase>
        <configuration>
            <target>
                <unzip src="target/${artifactId}-${version}.${packaging}" dest="target/ROOT/" />
                <delete file="target/ROOT/WEB-INF/classes/application.properties"/>
                <zip destfile="target/ROOT.war" basedir="target/ROOT" encoding="UTF-8"/>
                <delete dir="target/ROOT"/>
            </target>
        </configuration>
        <goals>
            <goal>run</goal>
        </goals>
    </execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

我将我的目标战争命名为 ROOT.war,因为我在 openshift PaaS 上使用 tomcat,所以我只需复制我的 ROOT.war 并推送到我的 openshift 存储库。就是这样