jar应用程序的外包属性

时间:2017-02-28 10:54:42

标签: java apache maven properties

我使用Apache Common Configuration:这很好,简单而有效

{
private static final String ENV_FILE = "basedir_conf_dir";
private static final String CONFIGURATION_FILE = "configuration/appli.properties";
private static final String DEFAULT_CONFIGURATION_FILE = "configuration/default-appli.properties";

private static ConfigurationManager configurationManager = null;
private CompositeConfiguration compositeConfiguration;

private ConfigurationManager() throws ConfigurationException {
    Parameters params1 = new Parameters();

    compositeConfiguration = new CompositeConfiguration();
    ConfigurationBuilder<PropertiesConfiguration> builder1 = new FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class).configure(params1.fileBased()
            .setURL(this.getClass().getClassLoader().getResource(CONFIGURATION_FILE)).setListDelimiterHandler(new DefaultListDelimiterHandler(';')));

    Configuration fichierConfigEnvironnement;
    fichierConfigEnvironnement = builder1.getConfiguration();

    compositeConfiguration.addConfiguration(fichierConfigEnvironnement);
    Parameters params2 = new Parameters();

    ConfigurationBuilder<PropertiesConfiguration> builder2 = new FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class).configure(params2.fileBased()
            .setURL(this.getClass().getClassLoader().getResource(DEFAULT_CONFIGURATION_FILE)).setListDelimiterHandler(new DefaultListDelimiterHandler(';')));

    Configuration fichierConfigEquipeDev = builder2.getConfiguration();
    compositeConfiguration.addConfiguration(fichierConfigEquipeDev);
}

现在,有了Maven我只想外化我的文件属性:

private static final String ENV_FILE = "basedir_conf_dir";
private static final String CONFIGURATION_FILE = "configuration/appli.properties";
private static final String DEFAULT_CONFIGURATION_FILE = "configuration/default-appli.properties";

我想:

  • 在我的erc环境中的src / main / resources / configuration
  • 在我的包中(在我的jar文件的同一级别)

-- package complete |_ myApplication.jar |_ appli.properties |_ default-appli.properties

你能帮我用Maven更改我的属性文件的网址吗?

1 个答案:

答案 0 :(得分:0)

把你的&#34; default.properties&#34;在src / main / resources / configuration中。然后在build部分中添加到你的pom.xml:

<resources>
        <resource>

            <directory>${basedir}/src/main/resources/configuration</directory>
            <includes>
                <include>default.properties</include>
            </includes>
            <targetPath>${project.build.directory}</targetPath>
        </resource>
        <resource>
             <directory>${basedir}/src/main/resources</directory>
        </resource>
</resources>

有关详细信息,请参阅Maven the reference guide