【问题标题】:Error import libraries from properties file on pom.xml从 pom.xml 上的属性文件导入库时出错
【发布时间】:2017-11-24 07:04:47
【问题描述】:

我正在尝试将我的版本从 pom.xml 移动到属性文件,但是这样做时,导入失败。

我的 pom.xml 是这样的:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.trial</groupId>
    <artifactId>books</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0.0</version>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                            <file>${basedir}/versions.properties</file>
                            </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <!-- SPRING -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        ...
    </dependencies>
</project>

虽然 pom.xml 上没有显示错误,但导入已经开始失败(当 pom.xml 上设置的属性时它工作正常):

import org.springframework.beans.factory.annotation.Autowired;

抛出:

错误:(6, 52) java: 包 org.springframework.beans.factory.annotation 不存在

versions.properties 看起来像:

springframework.version = 5.0.1.RELEASE

发生了什么事?我做错了什么?

非常感谢

【问题讨论】:

标签: maven jakarta-ee intellij-idea pom.xml maven-plugin


【解决方案1】:

你知道你可以使用这样的东西:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.trial</groupId>
    <artifactId>books</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
       <springframework.version>5.0.1.RELEASE</springframework.version>
    </properties>    

    <dependencies>
        <!-- SPRING -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        ...
    </dependencies>
</project>

将版本外部化到属性文件中没有任何好处。

【讨论】:

  • 是的,我知道,这就是我所说的工作正常的情况。但是我们想在外部配置多个具有相同版本的项目,所以最好的方法是创建一个versions.properties文件
  • 如果您想将它用于多个项目,请使用公司父 pom 并改用dependencyManagement ...或创建物料清单 (BOM)...尤其与 spring 相关。 Spring 已经为此目的提供了 bom...
  • 我会看看 DependencyManagement。谢谢
猜你喜欢
  • 2012-10-23
  • 2022-10-19
  • 2021-10-23
  • 2013-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
相关资源
最近更新 更多