替换pom.xml

时间:2016-05-12 14:59:42

标签: java maven templates

如何为Maven提供属性并强制它在pom.xml文件中实际替换它们?例如。我有一个“模板”pom.xml,我用它来部署包和一些依赖项。我希望替换artifactIdversion和其他变量,并将结果pom.xml上传。

使用命令行参数不起作用,例如mvn clean deploy -DartifactId=someArtifact会将jar上传到正确的位置,但上传的pom.xml包含占位符变量而不是实际值。

具体例子:

POM文件包含依赖项的这个定义:

    <dependencies>
        <dependency>
            <groupId>com.avast.melka</groupId>
            <artifactId>${runtimeId}</artifactId>
            <version>${runtimeVersion}</version>
        </dependency>
    </dependencies>

我希望Maven使用包含以下内容的POM部署相应的包:

<dependencies>
    <dependency>
        <groupId>com.avast.melka</groupId>
        <artifactId>actualIdValue</artifactId>
        <version>1.0.42</version>
    </dependency>
</dependencies>

我该怎么做?我能找到的最接近的是http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html - 但这涉及替换资源文件中的值,而不是POM,对我来说它似乎是一个不同的用例。

1 个答案:

答案 0 :(得分:0)

您可以使用replacer-plugin。有关插件的详细用法,请访问Internet。

警告1:我不认为替换artifactId是一种可维护的策略。但这是你的选择。

警告2:仅在仅在此特定版本上激活的配置文件中定义此插件。否则,这个插件执行会继承到子项目,这可能是一个坏主意。

您的配置应如下所示:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.res.Resources;

public class Fetch {

    final private static String DEPARTMENT = "Department";
    String [] departments ;


    public void fetch(Resources r , int resourceID) {
        String JsonString = readStringFromRaw(r, resourceID);
        //the whole josn file is a json object even if it starts with { and ends with } so...
        try {

            JSONObject mainObject = new JSONObject(JsonString);
            // the JSONObject throws a JSONException if there is something wrong with the syntax
            JSONArray department = mainObject.getJSONArray(DEPARTMENT);

            int length = department.length();

            departments = new String[length];

            JSONObject object;
            for(int i = 0 ; i < length ; i++){
            object = department.getJSONObject(i);
            departments[0] = object.getString(""+i+1);
            //this because you tagged the keys with 1 , 2 , 3 and so on.. so it has the value of the object that it is in + 1 . 
            //the reason I put "" empty quotations is because I want it a string so this is a way to cast the numbers to strings .

            }


        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public static String readStringFromRaw(Resources r, int resourceID) {
        InputStream is = r.openRawResource(resourceID);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder(1000);
        String line;
        try {
            while ((line = br.readLine()) != null)
                sb.append(line);
            br.close();
            is.close();
            return sb.toString();
        } catch (IOException e) {
            return e.getMessage();
        }
    }
}