在将pom文件放入maven存储库

时间:2016-10-28 15:41:09

标签: maven pom.xml

将项目安装到(本地)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文件?

1 个答案:

答案 0 :(得分:0)

<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”中生成我想要的内容。