简体   繁体   English

Java Release和Debug属性文件

[英]Java Release and Debug properties files

I am writing a Java application which is almost ready for release, but I don't know how to create different .properties files for Debug and Release. 我正在编写一个几乎可以发布的Java应用程序,但是我不知道如何为调试和发布创建不同的.properties文件。

Let me clarify this for you. 让我为您澄清一下。

I am storing the database host, username, password and other properties in the .properties files. 我将数据库主机,用户名,密码和其他属性存储在.properties文件中。

When I am writing and debugging the application these properties are configured to work with my development machine and database, but when the application is released they need to point to the release database and contain the release properties. 当我编写和调试应用程序时,这些属性被配置为可与我的开发机器和数据库一起使用,但是在发布应用程序时,它们需要指向发布数据库并包含发布属性。

Is there any way to achieve this with Java and Maven? 有什么方法可以使用Java和Maven来实现?

I once did something similar, I wanted to have several resources packs in a Java webapp: one for IDE development, one for local (but outside IDE) development for graphic designers, and finally one for release, with all the packing controlled by Maven. 我曾经做过类似的事情,我想在Java Webapp中有几个资源 :一个用于IDE开发,一个用于图形设计师的本地(但不是外部IDE)开发,最后一个用于发布,所有打包都由Maven控制。

My solution is to declare several extra resources folders in the <build> node, and tell Maven which one to pick up using profiles (like @biziclop already suggested you); 我的解决方案是在<build>节点中声明几个额外的资源文件夹,并告诉Maven使用配置文件选择哪个文件夹(例如@biziclop已经建议您); those folders are controlled through properties. 这些文件夹是通过属性控制的。

This is the POM I've used: 这是我使用的POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>
    <packaging>war</packaging>
    <name>...</name>

    <!-- My prerequisite was that when working in Eclipse no extra steps 
         should be required to make the IDE use the right configuration than
         Configure -> Convert to Maven Project, so I didn't like having 
         default settings in a profile that must be enabled in Eclipse project
         configuration -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <war-name>/</war-name>

        <!-- These solve the problem: AFAICT, each <resource /> is added to the final POM,
             so declaring a resources folder in a profile didn't exclude other resources 
             folders declared in the default (i.e. without profiles active) configuration.
             So, the solution is to change what Maven brings in from each folder depending
             on the profile currently active. What follows is the default, no-profile
             active configuration. -->
        <res.devel.includes>**/*</res.devel.includes>
        <res.devel.excludes></res.devel.excludes>

        <res.local.includes></res.local.includes>
        <res.local.excludes>*</res.local.excludes>

        <res.release.includes></res.release.includes>
        <res.release.excludes>*</res.release.excludes>
    </properties>

    <build>
        <resources><!-- Here I declare all the resources folders, so that they will all be shown in Eclipse. Property values drive what is included and excluded. -->
            <resource><!-- This is the default Maven main resource directory -->
                <directory>${basedir}/src/main/resources-local</directory>
                <filtering>true</filtering>
                <includes>
                    <include>${res.devel.includes}</include>
                </includes>

                <excludes>
                    <exclude>${res.devel.excludes}</exclude>
                </excludes>
            </resource>

            <resource><!-- This is the resources directory for when the WAR is deployed on a local standalone Tomcan installation (useful for web pages editing) -->
                <directory>${basedir}/src/main/resources-local</directory>
                <filtering>true</filtering>
                <includes>
                    <include>${res.local.includes}</include>
                </includes>

                <excludes>
                    <exclude>${res.local.excludes}</exclude>
                </excludes>
            </resource>

            <resource><!-- This is the resource directory for when the WAR will be deployed -->
                <directory>${basedir}/src/main/resources-release</directory>
                <filtering>true</filtering>
                <includes>
                    <include>${res.release.includes}</include>
                </includes>

                <excludes>
                    <exclude>${res.release.excludes}</exclude>
                </excludes>
            </resource>
        </resources>

        <plugins>
            <!-- Plugins configurations -->
        </plugins>
    </build>

    <dependencies>
        <!-- Dependencies declarations -->
    </dependencies>

    <profiles><!-- Here are the profiles. When working in Eclipse no profile is active, so the resources will be taken only from src/main/resources (as per default properties values). -->
        <profile>
            <id>local</id><!-- This is for when the WAR is deployed on a local standalone Tomcat instance (i.e. outside of Eclipse) -->
            <properties>
                <war-name>ROOT</war-name>

                <!-- The resources will be taken only from src/main/resources-local -->
                <res.devel.includes></res.devel.includes>
                <res.devel.excludes>*</res.devel.excludes>

                <res.local.includes>*</res.local.includes>
                <res.local.excludes></res.local.excludes>

                <res.release.includes></res.release.includes>
                <res.release.excludes>*</res.release.excludes>
            </properties>
        </profile>

        <profile>
            <id>release</id><!-- This is for when the WAR is deployed on the production server -->
            <properties>
                <war-name>ROOT</war-name>

                <!-- The resources will be taken only from src/main/resources-release -->
                <res.devel.includes></res.devel.includes>
                <res.devel.excludes>*</res.devel.excludes>

                <res.local.includes></res.local.includes>
                <res.local.excludes>*</res.local.excludes>

                <res.release.includes>*</res.release.includes>
                <res.release.excludes></res.release.excludes>
            </properties>
        </profile>
    </profiles>
</project>

You may get further details in my answer here . 您可以在此处获得我的答案的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM