如何从Maven POM中的文件设置构建属性?

use*_*914 8 java ant maven

我需要从项目外部的位置读取和过滤属性文件,比如$ {user.home} /my.properties.此属性文件如下所示:

res.dir=/my/stuff/here
resource.dir=C:/${res.dir}
bin.dir=${resource.dir}/bin
cfg.dir=${resource.dir}/config
Run Code Online (Sandbox Code Playgroud)

我必须在我的构建和运行时的应用程序中执行此操作.使用PROPERTY标记在Ant中很容易做到这一点.但是,在Maven中似乎没有一个好的方法.

到目前为止,我已经尝试了Maven <property>标签,Maven <filter>标签和其他标签的各种排列.我的构建失败或单元测试失败,或两者兼而有之.

如果我将这些属性硬编码到POM中,一切正常,所以我知道问题只是阅读属性.

我查看了properties-maven-plugin,但插件似乎不再被维护.

有没有办法做到这一点?

mab*_*aba 7

您可以简单地实现自己的工作maven-plugin,为您解决这个问题.

以下是具有以下结构的示例:

.
 |-- pom.xml
 |-- plugin
 |   `-- pom.xml
 |   `-- src
 |       `-- main
 |           `-- java
 `-- app
     `-- pom.xml
     `-- src
         `-- main
             `-- java
Run Code Online (Sandbox Code Playgroud)

您将需要创建一个魔咒,是以特性文件作为输入,然后传播特性到pom.xmlapp.在pom.xml实际上将不会被更新,但就在它的项目数据.

pom.xml

<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>com.stackoverflow</groupId>
    <artifactId>Q12082277</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <modules>
        <module>plugin</module>
        <module>app</module>
    </modules>

</project>
Run Code Online (Sandbox Code Playgroud)

plugin/pom.xml

<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>

    <parent>
        <groupId>com.stackoverflow</groupId>
        <artifactId>Q12082277</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Q12082277-plugin</artifactId>
    <packaging>maven-plugin</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-project</artifactId>
            <version>2.2.1</version>
        </dependency>
    </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

plugin/src/main/java/com/stackoverflow/Q12082277/plugin/PropertiesMojo.java

package com.stackoverflow.Q12082277.plugin;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 * @author maba, 2012-08-24
 *
 * @goal extract
 */
public class PropertiesMojo extends AbstractMojo {

    private Log log;

    /**
     * The current project representation.
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    /**
     * A properties file
     *
     * @parameter expression="${propertiesFile}"
     * @required
     */
    private File propertiesFile;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        log.info("Executing PropertiesMojo on " + propertiesFile.getAbsolutePath());

        try {
            Properties fileProperties = new Properties();
            fileProperties.load(new FileInputStream(propertiesFile));
            Properties projectProperties = project.getProperties();
            for (Object key : fileProperties.keySet()) {
                projectProperties.setProperty((String)key, (String) fileProperties.get(key));
            }
            project.getProperties().list(System.out);
        } catch (FileNotFoundException e) {
            throw new MojoFailureException("The file " + propertiesFile.getAbsolutePath() + " was not found!", e);
        } catch (IOException e) {
            log.error("");
        }

    }

    @Override
    public void setLog(Log log) {
        this.log = log;
    }
}
Run Code Online (Sandbox Code Playgroud)

您将使用以下插件 app/pom.xml

<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>

    <parent>
        <groupId>com.stackoverflow</groupId>
        <artifactId>Q12082277</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Q12082277-app</artifactId>

    <name>${project.artifactId}-${project.version}</name>

    <build>
        <plugins>
            <plugin>
                <groupId>com.stackoverflow</groupId>
                <artifactId>Q12082277-plugin</artifactId>
                <version>1.0-SNAPSHOT</version>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>extract</goal>
                        </goals>
                        <configuration>
                            <propertiesFile>${user.home}/my.properties</propertiesFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.stackoverflow.Q12082277.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

然后你将不得不添加以下app.properties将作为模板工作并获取我们刚刚从文件中读取的值并设置它们并创建一个app.properties可以从jar中访问的具体文件.

app/src/main/resources/app.properties

res.dir=${res.dir}
resource.dir=${resource.dir}
bin.dir=${bin.dir}
cfg.dir=${cfg.dir}
Run Code Online (Sandbox Code Playgroud)

最后这里是一个测试应用程序,它只是app.properties从类路径加载并打印结果.

app/src/main/java/com/stackoverflow/Q12082277/App.java

package com.stackoverflow.Q12082277;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author maba, 2012-08-23
 */
public class App {

    public static void main(String[] args) throws IOException {
        ClassLoader loader = App.class.getClassLoader();
        InputStream in = loader.getResourceAsStream("app.properties");
        Properties properties = new Properties();
        properties.load(in);
        properties.list(System.out);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以站在顶级目录中并执行

mvn install
Run Code Online (Sandbox Code Playgroud)

然后进入app文件夹并执行

mvn exec:java
Run Code Online (Sandbox Code Playgroud)

它会打印出来

-- listing properties --
resource.dir=C://my/stuff/here
cfg.dir=C://my/stuff/here/config
bin.dir=C://my/stuff/here/bin
res.dir=/my/stuff/here
Run Code Online (Sandbox Code Playgroud)

这正是你想要的.


mab*_*aba 5

我认为如果将属性文件转换为模板pom.xml文件并从using maven 资源过滤中获取属性会更好。

一个简单的设置可能如下所示

pom.xml

<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>com.stackoverflow</groupId>
    <artifactId>Q12082277</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>${project.artifactId}-${project.version}</name>

    <properties>
        <res.dir>/default/stuff/here</res.dir>
        <resource.dir>${res.dir}</resource.dir>
        <bin.dir>${resource.dir}/bin</bin.dir>
        <cfg.dir>${resource.dir}/config</cfg.dir>
    </properties>

    <dependencies>
    <!-- Your dependencies -->
    </dependencies>

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

</project>
Run Code Online (Sandbox Code Playgroud)

src/main/resources/app.properties

res.dir=${res.dir}
resource.dir=${resource.dir}
bin.dir=${bin.dir}
cfg.dir=${cfg.dir}
Run Code Online (Sandbox Code Playgroud)

src/main/java/com/stackoverflow/Q12082277/App.java

package com.stackoverflow.Q12082277;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author maba, 2012-08-23
 */
public class App {

    public static void main(String[] args) throws IOException {
        ClassLoader loader = App.class.getClassLoader();
        InputStream in = loader.getResourceAsStream("app.properties");
        Properties properties = new Properties();
        properties.load(in);
        properties.list(System.out);
    }
}
Run Code Online (Sandbox Code Playgroud)

System.out

-- listing properties --
resource.dir=/default/stuff/here
cfg.dir=/default/stuff/here/config
bin.dir=/default/stuff/here/bin
res.dir=/default/stuff/here
Run Code Online (Sandbox Code Playgroud)

pom.xml具有每个人都使用的默认属性。

如果您想覆盖这些值,请使用输入参数调用 Maven:

mvn install -Dres.dir=/my/stuff/here -Dresource.dir="C:/${res.dir}"
Run Code Online (Sandbox Code Playgroud)

System.out

-- listing properties --
resource.dir=C://my/stuff/here
cfg.dir=C://my/stuff/here/config
bin.dir=C://my/stuff/here/bin
res.dir=/my/stuff/here
Run Code Online (Sandbox Code Playgroud)

通过这种方式,每个人都将拥有相同的属性视图,并且在您自己的计算机上运行时,您可以根据需要覆盖它们。