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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26744060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:10:49  来源:igfitidea点击:

Exclude application.properties when generating war using spring boot and spring-boot-maven-plugin

javaeclipsespringmavenspring-boot

提问by amgohan

I am developing a web application using Spring Boot, and want to generate war instead of jar.

我正在使用 Spring Boot 开发 Web 应用程序,并且想要生成 war 而不是 jar。

It works very fine using the conversion from jar to war described here : http://spring.io/guides/gs/convert-jar-to-war/

使用此处描述的从 jar 到 war 的转换效果很好:http: //spring.io/guides/gs/convert-jar-to-war/

But I want to exclude the application.properties from the war, because I use @PropertySource(value = "file:${OPENSHIFT_DATA_DIR}/application.properties")to get the file path on production environment.

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

  • This method works when generating my war, but in eclipse I can't run my application because application.properties not copied at all to target/classes :

    <build>
        <resources> 
            <resource> 
                <directory>src/main/resources</directory> 
                <excludes> 
                    <exclude>application.properties</exclude> 
                </excludes> 
            </resource> 
         </resources> 
    </build>
    
  • This method doesn't work at all, I think that spring-boot-maven-plugin doesn't support 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>
    
  • 此方法在生成War时有效,但在 Eclipse 中我无法运行我的应用程序,因为 application.properties 根本没有复制到 target/classes :

    <build>
        <resources> 
            <resource> 
                <directory>src/main/resources</directory> 
                <excludes> 
                    <exclude>application.properties</exclude> 
                </excludes> 
            </resource> 
         </resources> 
    </build>
    
  • 这种方法根本行不通,我认为 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>
    

Have you another suggestion?

你还有什么建议吗?

Thanks

谢谢

回答by nav3916872

Try using the solution below. This will work:

尝试使用以下解决方案。这将起作用:

<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>

If you are using the above solution , while running the project in Eclipse IDE you may get error that the properties file is not found. To get rid of this you need to add the resources folder in Run as configuration.(Run configurations... -> Classpath -> User Entries -> Advanced... -> Add Folders)

如果您使用上述解决方案,在 Eclipse IDE 中运行项目时,您可能会收到找不到属性文件的错误。要摆脱这种情况,您需要在作为配置运行时添加资源文件夹。(运行配置... -> 类路径 -> 用户条目 -> 高级... -> 添加文件夹)

回答by mhenfhis

When running in Eclipse, at your Run Configuration, you need to specify the path of the propeties to Spring Boot:

在 Eclipse 中运行时,在您的运行配置中,您需要指定 Spring Boot 的属性路径:

--spring.config.location=${workspace_loc:/YOURPROYECTNAME}/src/main/resources/

回答by amgohan

The solution I added is to unzip my packaged war, delete the file application.properties and create a new war named ROOT.war using maven-antrun-plugin and run some ant tasks.

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

this is what i added to my plugins in pom.xml :

这是我在 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>

I named my target war as ROOT.war because I am using tomcat on openshift PaaS, so I just copy my ROOT.war and push to my openshift repo. That's it

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

回答by O-OF-N

What I understand from your question is, you want to use application.properties for your development. But you dont want to use it for production.

我从您的问题中了解到,您想使用 application.properties 进行开发。但是您不想将其用于生产。

I would suggest using Spring profilesto achieve this. In your properties file

我建议使用Spring 配置文件来实现这一点。在您的属性文件中

  1. Create a profile for development. Put all your development properties under it.
  2. Do not create a profile for production in your properties file.
  3. When you are developing, set active profile to development, so that the properties are loaded from your application.propertiesfile.
  4. When you run it in production, set active profile to Production. Though application.properties will be loaded into your WAR, since there is no profile for production, none of the properties will be loaded.
  1. 创建用于开发的配置文件。把你所有的开发属性放在它下面。
  2. 不要在您的属性文件中为生产创建配置文件。
  3. 开发时,将活动配置文件设置为 development,以便从application.properties文件加载属性。
  4. 在生产中运行它时,将活动配置文件设置为生产。尽管 application.properties 将加载到您的 WAR 中,但由于没有用于生产的配置文件,因此不会加载任何属性。

I have done something similar using YML. I am sure there must be a way to do the same thing using .propertiesfile too.

我使用 YML 做了类似的事情。我相信一定有一种方法可以使用.properties文件来做同样的事情。

spring:
  profiles.active: development
--
spring:
  profiles: development
something:
  location: USA
  unit1: Test1
  unit2: Test2

You could change the profile in run time using

您可以使用在运行时更改配置文件

-Dspring.profiles.active=production

回答by fabiohbarbosa

Try to using this solution:

尝试使用此解决方案:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring.version}</version>
    <configuration>
        <addResources>false</addResources>
    </configuration>
</plugin>

<addResources>false</addResources>will keep properties when you run mvn spring-boot:run

<addResources>false</addResources>将在您运行 mvn spring-boot:run 时保留属性

回答by Prateep Gedupudi

It is keeping the resource application.properiteseven after generating jar package even with <addResources>false</addResources>

application.properites即使在生成 jar 包后,它仍然保留资源<addResources>false</addResources>