0

私が取り組んでいるプロジェクトでは、さまざまなマルチモジュール プロジェクトが並行して開発されており、そのうちのいくつかは他のプロジェクトに依存しています。このため、開発中の内部依存関係などにバージョン範囲を使用して、[0.0.1,)常に最新のスナップショット バージョンに対して作業できるようにしています。(これがベスト プラクティスとは見なされないことは理解していますが、少なくとも今のところ、現在のプロジェクト構造にこだわっています。) リリースを実行するときに、すべてのバージョン範囲が に置き換えられRELEASEてコンパイルされるように、ビルド プロファイルが設定されています。リリースされた最新バージョン。

LATESTアーティファクトをローカルにインストールする場合、<latest>maven-metadata-local.xml 内のタグは更新されず、指定するLATESTと Artifactory サーバーにデプロイされた最後のバージョンが取得されるため、範囲を使用する必要があります。ただし、範囲の問題は、最新バージョンを判別できるようにするために、ビルド プロセスでアーティファクトのすべてのバージョンのすべてのメタデータ ファイルをダウンロードする必要があるように見えることです。私たちのプロジェクトが進行するにつれて、ますます多くのバージョンとアーティファクトが蓄積されるため、ビルドにかかる時間はますます長くなります。指定LATESTすることでこれを回避できますが、ローカル アーティファクト インストールからの変更は一般的に取得されないことを意味します。

<latest>アーティファクトをローカルにインストールするときに、maven-metadata-local.xml ファイルのタグを更新する方法はありますか?

4

3 に答える 3

1

私は同じ問題を抱えていたので、それを処理するためのmavenプラグインを書きました。これはかなり極端な回避策ですが、機能します。

Maven プラグインの作成に関するドキュメントは、The Apache Maven Projectにあります。コマンド ライン アーキタイプからプラグイン プロジェクトを作成し、このモジョをプロジェクトに追加するだけです。

/**
 * Inserts a "latest" block into the maven-metadata-local.xml in the user's local
 * repository using the currently configured version number.
 * 
 * @version Sep 23, 2013
 */
@Mojo( name = "latest-version", defaultPhase = LifecyclePhase.INSTALL )
public class InstallLatestVersionMojo extends AbstractMojo {

/**
 * Location of the .m2 directory
 */
@Parameter( defaultValue = "/${user.home}/.m2/repository", property = "outputDir", required = true )
private File repositoryLocation;

@Parameter( defaultValue = "${project.groupId}", property = "groupId", required = true )
private String groupId;

@Parameter( defaultValue = "${project.artifactId}", property = "artifactId", required = true )
private String artifactId;

/**
 * Version to use as the installed version
 */
@Parameter( defaultValue = "${project.version}", property = "version", required = true )
private String version;

public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        // Fetch the xml file to edit from the user's repository for the project
        File installDirectory = getInstallDirectory(repositoryLocation, groupId, artifactId);
        File xmlFile = new File(installDirectory, "maven-metadata-local.xml");
        Document xml = getXmlDoc(xmlFile);

        if (xml != null) {
            // Fetch the <latest> node
            Node nodeLatest = getNode(xml, "/metadata/versioning/latest");
            if (nodeLatest == null) {
                // If <latest> does not yet exist, insert it into the <versioning> block before <versions>
                nodeLatest = xml.createElement("latest");
                Node versioningNode = getNode(xml, "/metadata/versioning");
                if (versioningNode != null) {
                    versioningNode.insertBefore(nodeLatest, getNode(xml, "metadata/versioning/versions"));
                }
            }
            // set the version on the <latest> node to the newly installed version
            nodeLatest.setTextContent(version);
            // save the xml
            save(xmlFile, xml);
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private void save(File xmlFile, Document xml) throws TransformerFactoryConfigurationError, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    Result output = new StreamResult(xmlFile);
    Source input = new DOMSource(xml);
    transformer.transform(input, output);
}

private Node getNode(Document source, String path) throws XPathExpressionException{
    Node ret = null;
    XPathExpression xPath = getPath(path);
    NodeList nodes = (NodeList) xPath.evaluate(source, XPathConstants.NODESET);
    if(nodes.getLength() > 0 ) {
        ret = nodes.item(0);
    }
    return ret;
}

private XPathExpression getPath(String path) throws XPathExpressionException{
    XPath xpath = XPathFactory.newInstance().newXPath();
    return xpath.compile(path);
}

private File getInstallDirectory(File repositoryLocation, String groupId, String artifactId) {
    String group = groupId.replace('.', '/');
    return new File(repositoryLocation, group + "/" + artifactId);
}

private Document getXmlDoc(File xmlFile) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    return dBuilder.parse(xmlFile);
}

}
于 2013-09-25T18:26:29.643 に答える
1

SNAPSHOT を使用している場合は、バージョン範囲を使用しないことを除けば、バージョン範囲は必要ありません (非常にまれな状況でのみ)。バージョン範囲を使用すると、ビルドは再現できなくなります。これは、私の意見では、どのような状況でも回避する必要があります。

ただし、次のようなものを使用できます。

<version>[1.2.3,)</version

しかし、それがいくつかの問題を引き起こしたことにすでに気付いているようですが、それに応じてプロジェクトの pom ファイルを更新する代わりに、versions-maven-plugin を使用することをお勧めします。

mvn clean versions:use-latest-versions scm:checkin deploy -Dmessage="update versions" -DperformRelease=true

これは、Jenkins などの CI ソリューションで処理できます。しかし、あなたはいくつかの基本的なことを間違っているという印象を受けました。特に、バージョン範囲を使用する必要がある場合。

于 2013-06-14T15:32:24.787 に答える
0

これらの内部依存関係を 1 つのリアクター po でモジュールとして定義するのはどうですか? そうすれば、jar に対してではなく、コンパイルされたソース (ターゲット/クラス内) に対してコンパイルされ、常に最新のコードが得られます。

于 2013-06-15T13:17:57.893 に答える