2016-04-26 45 views
0

我写一个Java应用程序,这几乎是准备好发布,但我不知道如何创建不同.properties文件调试和发布。的Java版本和调试属性文件

让我澄清一下这个给你。

我存储在.properties文件的数据库主机,用户名,密码和其他属性。

当我编写和调试这些属性都配置了自己的计算机和数据库使用的应用程序,但应用程序被释放时,他们需要指向发布数据库,并含有释放性能。

有什么办法与Java和Maven实现这一目标?

+1

http://maven.apache.org/guides/introduction/introduction-to-profiles.html :)和http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html – 2016-04-26 16:55:34

+0

你应该看看Maven中的配置文件。 – biziclop

回答

1

我曾经做过类似的事情,我想在一个Java webapp中有几个资源:一个用于IDE开发,一个用于图形设计师的本地(但不包括IDE)开发,最后一个用于发布包装由Maven控制。

我的解决方案是在<build>节点中声明几个额外的资源文件夹,并告诉Maven使用配置文件选择哪一个文件夹(如@biziclop已经建议您);这些文件夹通过属性进行控制。

这是我用过的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> 

你可以在我的答案here获得进一步的细节。

+0

谢谢你的答案和代码示例。我只是测试它,它的效果很好。现在我看它,看起来很简单,但我没有太多的Maven经验,所以对我来说是新的。有没有办法从上面的例子中在父'pom.xml'文件中提取'properties','resources'和'profiles'?问题是我有几个模块,我想将这些资源文件保存在这些模块中,但是我必须将所有这些代码复制粘贴到它们的所有pom文件中。 –

+0

@IvanStoyanov我想是的,因为Maven非常灵活;但我还没有父母POM的经验,所以我不能帮忙。你应该问一个单独的问题,我会对它很感兴趣。 – watery