【问题标题】:Replace properties with it's values in pom.xml before putting the pom file into maven repository在将 pom 文件放入 maven 存储库之前,用 pom.xml 中的值替换属性
【发布时间】:2017-03-11 13:17:48
【问题描述】:

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

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

这是我最小化的 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”下面的内容:

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

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

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

【问题讨论】:

  • 您尝试解决什么问题。从属性中获取项目版本不是好的行为。每个构建都应该产生相同的应用程序,并且应该在没有特殊配置的任何系统上复制。
  • 我要为不同的平台构建,所以版本应该不同。
  • 在这种情况下,分类器是正确的选择,而不是版本。

标签: maven pom.xml


【解决方案1】:
<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”中产生我想要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-27
    • 2017-03-30
    • 2012-10-23
    • 1970-01-01
    • 2016-01-12
    • 2015-08-22
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多