簡體   English   中英

如何從Maven POM中的文件設置構建屬性?

[英]How to set build properties from a file in Maven POM?

我需要從項目外部的位置讀取和過濾屬性文件,比如$ {user.home} /my.properties。 此屬性文件如下所示:

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

我必須在我的構建和運行時的應用程序中執行此操作。 使用PROPERTY標記在Ant中很容易做到這一點。 但是,在Maven中似乎沒有一個好的方法。

到目前為止,我已經嘗試了Maven <property>標記,Maven <filter>標記以及其他標記的各種排列。 我的構建失敗或單元測試失敗,或兩者兼而有之。

如果我將這些屬性硬編碼到POM中,一切正常,所以我知道問題只是閱讀屬性。

我查看了properties-maven-plugin,但插件似乎不再被維護。

有沒有辦法做到這一點?

你可以簡單地實現自己的maven-plugin來為你處理這個問題。

以下是具有以下結構的示例:

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

您需要創建一個Mojo,將屬性文件作為輸入,然后將屬性傳播到apppom.xml 實際上不會更新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>

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>

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

您將使用以下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作為模板,並獲取我們剛剛從文件中讀取的值並設置它們並創建一個可以從jar中訪問的具體文件app.properties

app/src/main/resources/app.properties

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

最后這里是一個測試應用程序, app.properties從類路徑加載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);
    }
}

現在,您可以站在頂級目錄中並執行

mvn install

然后進入app文件夾並執行

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

這正是你想要的。

我認為如果將屬性文件轉換為模板文件並使用maven資源過濾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>

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

src/main/resources/app.properties

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

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);
    }
}

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

pom.xml將具有每個人使用的默認屬性。

如果要覆蓋值,則使用輸入參數調用maven:

mvn install -Dres.dir=/my/stuff/here -Dresource.dir="C:/${res.dir}"

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

通過這樣做,每個人都可以擁有相同的屬性視圖,如果你想在自己的機器上運行,你可以覆蓋它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM