当前位置: 首页 > 知识库问答 >
问题:

Java发布和调试属性文件

宋子辰
2023-03-14

我正在编写一个即将发布的Java应用程序,但我不知道如何创建不同的。用于调试和发布的属性文件。

让我为你澄清一下。

我将数据库主机、用户名、密码和其他属性存储在. Properties文件中。

当我编写和调试应用程序时,这些属性被配置为与我的开发机器和数据库一起使用,但当应用程序发布时,它们需要指向发布数据库并包含发布属性。

Java和马文有什么办法做到这一点吗?


共有1个答案

丁恩
2023-03-14

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

我的解决方案是在<代码>

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

你可以在我的回答中得到更多细节。

 类似资料:
  • 问题内容: 我在加载时遇到问题 在同一个文件夹中。 我在dist文件夹中有一个myProject.jar,在conf文件夹中有test.xml和test.properties。 要加载xml,我正在使用 但是我在加载属性文件时遇到问题 属性文件没有任何作用。 任何帮助表示赞赏。 问题答案: 为什么不取出文件并使用 上面的代码将获取属性文件并将其加载到属性对象中。

  • 我正在开发一个android应用程序。我有一个java文件,我想在调试和发布构建类型中不同地使用它。我知道我可以有不同的资源文件用于调试和发布版本。但是对Java文件也可以这样做吗?如何实现这一点?

  • 一旦源代码tarball已经从稳定的发布分支产生,发布过程公共部分便已经开始。但是在tarball进入公开之前,必须经过少量开发者的确认,通常需要三位或者更多。确认不仅仅是检测发布的明显缺陷;理想情况下,开发者应该下载tarball,在干净的系统上构建并安装,运行回归测试包Chapter 8, 管理志愿者的(见the section called “自动测试”),然后执行一些手工测试。假如通过了这

  • 问题内容: 我正在尝试使用ant在命令行上生成apk。我可以使用ant clean,但是对于ant调试和ant release命令,我得到以下错误。 建立失败 C:\ Android \ sdk \ tools \ ant \ build.xml:649:执行此行时发生以下错误:C:\ Android \ sdk \ tools \ ant \ build.xml:694:执行失败:java.io

  • 本文向大家介绍Android 布局文件Layout XML属性,包括了Android 布局文件Layout XML属性的使用技巧和注意事项,需要的朋友参考一下 Layout对于迅速的搭建界面和提高界面在不同分辨率的屏幕上的适应性具有很大的作用。这里简要介绍Android的Layout和研究 一下它的实现。 Android有Layout:FrameLayout,LinearLayout,TableL

  • 本文重要内容 CSS的单位 字体属性 文本属性 定位属性:position、float、overflow等 CSS的单位 html中的单位只有一种,那就是像素px,所以单位是可以省略的,但是在CSS中不一样。CSS中的单位是必须要写的,因为它没有默认单位。 绝对单位: 1 in=2.54cm=25.4mm=72pt=6pc。 各种单位的含义: in:英寸Inches (1 英寸 = 2.54 厘米