在将 pom 文件放入 maven 存储库之前,用 pom.xml 中的值替换属性 [英] Replace properties with it's values in pom.xml before putting the pom file into maven repository

查看:76
本文介绍了在将 pom 文件放入 maven 存储库之前,用 pom.xml 中的值替换属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将项目安装到(本地)maven 存储库时,我想将所有属性占位符替换为其当前的实际值,这有可能吗?

总而言之,我正在尝试在 maven 中创建一个可自定义的项目版本,以解决传递依赖项的一些问题.

All in all I'm trying to create a customizable project version in maven, in order to resolve some problems with the transitive dependencies.

这是我最小化的 pom.xml:

Here is my minimized 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.foo</groupId>
<artifactId>T</artifactId>
<version>${custom.version}</version>

<properties>
    <custom.version>TEST</custom.version>
</properties>

<build>
    <resources>
        <resource>
            <directory>${basedir}</directory>
            <includes>
                <include>pom.xml</include>
            </includes>
            <filtering>true</filtering>
            <targetPath>${basedir}</targetPath>
        </resource>
    </resources>
</build>

在mvn clean install"之后,我想在本地maven仓库中拥有~/.m2/repository/com/foo/T/TEST/T-Test.pom"以下内容:

After "mvn clean install" I would like to have in the local maven repository here "~/.m2/repository/com/foo/T/TEST/T-Test.pom" following contents:

<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.foo</groupId>
<artifactId>T</artifactId>
<version>TEST</version>

<properties>
    <custom.version>TEST</custom.version>
</properties>

<build>
    <resources>
        <resource>
            <directory>doesn't matter</directory>
            <includes>
                <include>pom.xml</include>
            </includes>
            <filtering>true</filtering>
            <targetPath>doesn't matter</targetPath>
        </resource>
    </resources>
</build>

因此,在将项目安装到 Maven 存储库之前,${custom.version} 占位符应替换为其当前值TEST".

So the ${custom.version} placeholder should be replaced by it's current value "TEST", before installing the project into maven repository.

但我得到的 - 是一个空文件,即使在当前项目目录中,可能是因为该文件被锁定并且 maven 无法覆盖它.

But all I get - is an empty file, even in the current project directory, probably because the file is locked and maven can't override it.

是否有可能安装/部署具有解析属性值的 pom 文件?

推荐答案

<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.foo</groupId>
<artifactId>T</artifactId>
<packaging>jar</packaging>
<version>${custom.version}</version>

<properties>
    <custom.version>TEST2</custom.version>
</properties>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>./</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                        <outputDirectory>target</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <configuration>
                <pomFile>target/pom.xml</pomFile>
            </configuration>
        </plugin>
    </plugins>
</build>

在~/.m2/repository/com/foo/T/TEST/T-Test.pom"中生成我想要的内容.

Produces in "~/.m2/repository/com/foo/T/TEST/T-Test.pom" what I want.

这篇关于在将 pom 文件放入 maven 存储库之前,用 pom.xml 中的值替换属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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