有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用maven使用参数构建java项目

我想使用Maven和参数构建一个Java项目,这些参数在每次构建中都会发生变化。例如,一个参数是在程序内部检查的密钥

一旦构建项目,就不能读取参数。尝试了不同的方法,包括来自org的插件。科德豪斯。mojo…但存在“生命周期未涵盖插件执行”的问题

    /****************************/
    /**read property values     */
    /****************************/
    //Create a new property list
    Properties properties = new Properties();
    //create a input strem
    InputStream inputStream = null;
    //try to read the property file
    try {
        String filename ="restApi.properties";
        inputStream = Main.class.getClassLoader().getResourceAsStream(filename);
        if(inputStream==null) {
            System.out.println("Unable to read required properties");
        }
        properties.load(inputStream);
        System.out.println(properties.getProperty("property_mainUrlValue"));
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

我的pom。xml

<properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <property_mainUrlValue>property_mainUrlValue</property_mainUrlValue>
<properties>

<build>
    <sourceDirectory>src</sourceDirectory>
    <pluginManagement>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0-alpha-2</version>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>write-project-properties</goal>
                    </goals>
                    <configuration>
                        <outputFile>${project.build.outputDirectory}/restApi.properties</outputFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    </pluginManagement>
</build>

error shown in eclipse

     Unable to read required properties
     Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
at itAsset.Main.main(Main.java:58)

共 (1) 个答案

  1. # 1 楼答案

    我猜您正在搜索Maven所说的内容(出于某种我不理解的原因)“过滤”

    其基本思想是:

    通过将以下配置包括到pom.xml中来启用该功能:

    ...
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
    ...
    

    这导致当您运行mvn resources:resources -Dkey="mmm123",并且有一个包含行的资源src/main/resources/restApi.properties

    appKey=${key}
    

    然后Maven将在target/classes/restApi.properties中创建一个资源输出,其中包含

    appKey=mmm123
    

    详细说明如下:http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html