如何在Maven的POM文件中设置构建属性? [英] How to set build properties from a file in Maven POM?

查看:246
本文介绍了如何在Maven的POM文件中设置构建属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要阅读和筛选的属性从文件的位置我的项目外,说的$ {}的user.home /my.properties。此属性文件看起来是这样的:

I need to read and filter a properties file from a location outside my project, say ${user.home}/my.properties. This properties file looks like this:

res.dir=/my/stuff/here
resource.dir=C:/${res.dir}
bin.dir=${resource.dir}/bin
cfg.dir=${resource.dir}/config

我必须这样做,既在我的构建和我的应用程序在运行时。这是很容易在蚂蚁的事,使用属性标记。然而,似乎没有要做到在Maven的一个好办法。

I have to do this in both my build and in my application when it runs. This is easy to do in Ant, using the PROPERTY tag. However, there doesn't seem to be a good way to do it in Maven.

到目前为止,我已经试过Maven的<性> 标签,Maven的<滤光器> 标记其它标记的各种排列。无论是我的构建失败或单元测试失败,或两者兼而有之。

So far I have tried the Maven <property> tag, the Maven <filter> tag and various permutations of other tags. Either my build fails or the unit tests fail, or both.

如果我硬code这些属性到POM,一切正常,所以我知道这个问题是只是阅读的属性。

If I hardcode these properties into the POM, everything works, so I know the problem is just reading the properties.

我看着属性 - Maven的插件,但该插件似乎不再是维护。

I looked at the properties-maven-plugin but the plugin no longer seems to be maintained.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

您可以简单地实现自己的 Maven的插件,将照顾这对你。

You could simply implement your own maven-plugin that will take care of this for you.

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

Here is an example with the following structure:

.
 |-- pom.xml
 |-- plugin
 |   `-- pom.xml
 |   `-- src
 |       `-- main
 |           `-- java
 `-- app
     `-- pom.xml
     `-- src
         `-- main
             `-- java

您需要创建一个魔咒,是以属性文件的输入,然后传播属性的的pom.xml 应用程序。在的pom.xml 实际上将不会被更新,但只是其中的项目数据。

You will need to create a Mojo that takes the properties file as an input and then propagates the properties to the pom.xml of the app. The pom.xml will actually not be updated but just the project data in it.

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

插件/ 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>

插件/ src目录/主/ JAVA / COM /计算器/ Q12082277 /插件/ 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;
    }
}

您将使用这个插件从以下应用程序/ pom.xml的

You will use this plugin from the following 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>

然后你将不得不添加以下 app.properties ,将工作作为模板,走,我们刚刚从文件中读取值并将它们与创建一个具体的文件 app.properties ,这将是从罐子内到达。

And then you will have to add the following app.properties that will work as a template and take the values that we have just read from file and set them and create a concrete file app.properties that will be reachable from within the jar.

应用程序/ src目录/主/资源/ app.properties

res.dir=${res.dir}
resource.dir=${resource.dir}
bin.dir=${bin.dir}
cfg.dir=${cfg.dir}

和终于在这里是刚刚加载 app.properties 从classpath并输出结果。

And finally here is a test application that just loads the app.properties from the classpath and prints the result.

应用程序/ src目录/主/ JAVA / COM /计算器/ 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);
    }
}

现在你能忍受在顶级目录并执行

Now you can stand in the top directory and execute

mvn install

然后向下进入应用文件夹并执行

mvn exec:java

和它将打印

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

这是你想要的是什么。

Which is exactly what you wanted.

这篇关于如何在Maven的POM文件中设置构建属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆