首页 > 解决方案 > Maven flatten 插件不工作不能代替我的版本属性

问题描述

出于某种原因,当我执行 mvn install 并检查我的本地 repo 并查看那里的 pom 时,我没有看到 version 变量被替换为值。

我在属性中使用带有版本的pom

<properties>
    <maven.javadoc.skip>true</maven.javadoc.skip>
    <revision>19.3.29</revision>
</properties>

它还包含 flatten 插件条目:

          <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>1.2.2</version> 
                <configuration>
                    <updatePomFile>true</updatePomFile>
                    <flattenMode>resolveCiFriendliesOnly</flattenMode>
                </configuration>
                <executions>
                    <execution>
                        <id>flatten</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>flatten</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>flatten.clean</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

pom使用${revision} 作为版本和 ${project.version} 的值作为maven推荐的依赖项:

<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>
    <artifactId>analysisModule</artifactId>
    <version>${revision}</version>
    <parent>
        <groupId>com.mycompany.analysis</groupId>
        <version>1.0.0</version>
        <artifactId>autoav</artifactId>
        <relativePath>../autoAV</relativePath>
    </parent>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
    <dependencies>
      <dependency>
        <groupId>com.mycompany.analysis</groupId>
        <artifactId>analysisCore</artifactId>
        <version>${project.version}</version>
      </dependency>
    </dependencies>
</project>

在我“mvn install”之后查看本地 repo 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>
        <artifactId>analysisJdbc</artifactId>
        <version>${revision}</version>
        <description>analysis of jdbc sources</description>
        <parent>
                <groupId>com.mycompany.analysis</groupId>
                <version>1.0.0</version>
                <artifactId>autoav</artifactId>
                <relativePath>../autoAV</relativePath>
        </parent>
        <build>
                <resources>
                        <resource>
                                <directory>src/main/resources</directory>
                                <filtering>true</filtering>
                        </resource>
                </resources>
        </build>
        <dependencies>
                <dependency>
                        <groupId>com.mycompany.analysis</groupId>
                        <artifactId>analysisCore</artifactId>
                        <version>${project.version}</version>
                </dependency>
...
</project>

${revision} & ${project.version}不应该用实际数字代替吗?这不是使用 flatten 插件的全部内容吗?

我跟着这个 https://www.mojohaus.org/flatten-maven-plugin/examples/example-central-version.html

添加我的 mvn install 输出(跳过测试)(maven 版本 3.3.9):

02:12:03 :/code/thedrs/gitrepos/analysis/analysis/analysisJdbc  (main) $ mvn install  -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building analysisJdbc 19.3.30
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ analysisJdbc ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ analysisJdbc ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ analysisJdbc ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ analysisJdbc ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ analysisJdbc ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ analysisJdbc ---
[INFO] Building jar: /code/thedrs/gitrepos/analysis/analysis/analysisJdbc/target/analysisJdbc-19.3.30.jar
[INFO] META-INF/maven/com.mycompany.analysis/analysisJdbc/pom.xml already added, skipping
[INFO] META-INF/maven/com.mycompany.analysis/analysisJdbc/pom.properties already added, skipping
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ analysisJdbc ---
[INFO] Installing /code/thedrs/gitrepos/analysis/analysis/analysisJdbc/target/analysisJdbc-19.3.30.jar to /code/thedrs/.m2/com/mycompany/analysis/analysisJdbc/19.3.30/analysisJdbc-19.3.30.jar
[INFO] Installing /code/thedrs/gitrepos/analysis/analysis/analysisJdbc/pom.xml to /code/thedrs/.m2/com/mycompany/analysis/analysisJdbc/19.3.30/analysisJdbc-19.3.30.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.147 s
[INFO] Finished at: 2020-05-06T02:12:40-07:00
[INFO] Final Memory: 14M/356M
[INFO] ------------------------------------------------------------------------

标签: mavenflatten-maven-plugin

解决方案


您需要将插件从 移动<pluginManagement><plugins><plugins>. 或者,您可以将其保留在那里,只需向该<plugins>部分添加一个简短的插件定义(包括 groupId 和 artifactId)。

<pluginManagement>只是为了预配置版本号和配置。插件在<pluginManagement>构建期间不会运行,除非它们重新出现在<plugins>.


推荐阅读