【问题标题】:Read pom dependency version from properties file从属性文件中读取 pom 依赖版本
【发布时间】:2020-11-02 08:35:36
【问题描述】:

试图从属性文件中读取版本但遇到异常

  1. 生命周期配置未涵盖插件执行:org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:read-project-properties (执行:默认,阶段:初始化)
  2. 缺少工件 org.springframework.cloud:spring-cloud-starter-zipkin:jar:${zipkin-version}

下面是pom.xmlproperties 文件

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath /> 
    </parent>
    <groupId>com.wtw</groupId>
    <artifactId>Demo Project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>AlertNotification</name>
    <description>Demo</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0-alpha-2</version>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <files>
                        <file>C:\Workspace\version.properties</file>
                    </files>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>           
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
            <version>${zipkin-version}</version>
        </dependency>
        
    </dependencies> 

</project>




version.properties file
zipkin-version=2.2.2.RELEASE

【问题讨论】:

标签: java maven pom.xml


【解决方案1】:

在执行插件之前解决依赖关系。

因此,您使用 properties-maven-plugin 读取的属性在 &lt;dependencies&gt; 部分中不可用。

如果您想通过属性设置依赖版本,则必须在 POM、命令行或settings.xml 中设置此属性。

【讨论】:

  • 意味着我需要把所有的属性文件放在settings.xml 中?或者有什么方法可以将外部属性文件放在 settings.xml 中?如果你能举个例子,那会很有帮助
  • 不,您不能将外部文件放入settings.xml,我不确定将属性放在那里是否明智。你到底想达到什么目的?谁应该提供版本?为什么不在 POM 中设置?
  • 我有 4 5 个模块,它们是单独的模块,它们没有任何父模块所以每个模块都有相同的 jar,所以在一些常见的地方尝试 jar 版本,这样我就不需要为 jar 版本保留每个模块,我都需要维护在一个地方,即属性文件。我将在所有模块中引用这个属性文件
  • 为什么不为此使用 BOM?您可以将 dependencyManagement 放入 BOM 中,然后将 import 放入所有项目中的 BOM 中。
  • 您先构建 BOM mvn install,然后在其他项目中使用正确版本的 BOM。
【解决方案2】:

您使用的是旧版本的插件 (1.0-alpha-2),请将其更新为 latest 1.0.0

然后确保文件version.properties 位于文件夹C:\Workspace 中。无论如何,使用最新版本的插件,如果找不到文件,您应该会收到正确的错误消息。

还有一个建议:spring-cloud-starter-zipkin 属于 org.springframework.cloud 组,该组遵循另一个版本。声明该依赖项的建议方法如下:

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- ... -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zipkin</artifactId>
    </dependency>
    <!-- ... -->
</dependencies>

【讨论】:

    猜你喜欢
    • 2017-05-24
    • 1970-01-01
    • 2020-07-12
    • 2011-10-31
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    相关资源
    最近更新 更多