Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用maven插件从.properties读取值_Maven - Fatal编程技术网

如何使用maven插件从.properties读取值

如何使用maven插件从.properties读取值,maven,Maven,如何使用maven提供的插件从.properties文件中读取属性,对于我当前的一个项目,我总是在pom.xml的标记中设置属性,但这里我的要求是,我将在一些.properties文件中为ex:dev.properties设置所有属性。它包含以下内容 spring.package=org.springframework spring.artifact=spring-core spring.version=3.0.5.RELEASE 因此,现在我想将上述属性设置到pom.xml中,如下所示: &

如何使用maven提供的插件从
.properties
文件中读取属性,对于我当前的一个项目,我总是在
pom.xml
标记中设置属性,但这里我的要求是,我将在一些
.properties
文件中为ex:dev.properties设置所有属性。它包含以下内容

spring.package=org.springframework
spring.artifact=spring-core
spring.version=3.0.5.RELEASE
因此,现在我想将上述属性设置到
pom.xml
中,如下所示:

<dependencies>
        <dependency>
            <groupId>spring.package</groupId>
            <artifactId>spring.artifact</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
但是
spring-core-3.0.5.RELEASE jar
在maven存储库中可用。 maven中是否有其他插件可以执行同样的操作,或者我是否错过了任何其他配置

如果我错了,请改正


谢谢。

请在下面找到更正的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.ram</groupId>
<artifactId>DynamicProperties</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
    <spring-version>spring.version</spring-version>
</properties>
<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>
                        <urls>
                            <url>file:///${basedir}\src\main\resources\config.properties</url>
                        </urls>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>

4.0.0
com.ram
动态属性
0.0.1-快照
spring.version
org.codehaus.mojo
属性maven插件
1.0.0
初始化
读取项目属性
文件://${basedir}\src\main\resources\config.properties
maven编译器插件
3.5.1
1.7
1.7
org.springframework
弹簧芯
${spring.version}

问题出在-

<spring.version>spring.version</spring.version>
spring.version
标签。应该是的

<spring-version>spring.version</spring-version>
spring.version
内部属性标签


BR

请在下面找到更正的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.ram</groupId>
<artifactId>DynamicProperties</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
    <spring-version>spring.version</spring-version>
</properties>
<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>
                        <urls>
                            <url>file:///${basedir}\src\main\resources\config.properties</url>
                        </urls>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>

4.0.0
com.ram
动态属性
0.0.1-快照
spring.version
org.codehaus.mojo
属性maven插件
1.0.0
初始化
读取项目属性
文件://${basedir}\src\main\resources\config.properties
maven编译器插件
3.5.1
1.7
1.7
org.springframework
弹簧芯
${spring.version}

问题出在-

<spring.version>spring.version</spring.version>
spring.version
标签。应该是的

<spring-version>spring.version</spring-version>
spring.version
内部属性标签


BR

首先,当Maven读取pom.xml时,它将立即用指定为的实际值替换占位符,例如
${spring.version}

<properties>
  <spring.version>spring.version</spring.version>
</properties>

spring.version
因此,以后通过插件更改值太晚了!但是,如果此时没有可用的值,Maven将不会这样做。因此,您可以从pom.xml中删除这些属性,并让Maven插件在以后的构建生命周期中定义它们。这通常可以解决插件覆盖的属性的使用问题,但是

其次,可能还有另一个问题:Maven甚至在执行任何插件之前都可能会解决依赖项(包括它们的版本),这将阻止您完成这一切。如果是这种情况,您可以将属性移动到配置文件,而不是


3.0.4-发布
发展
3.0.5-发布
戳
3.0.1-发布

首先,当Maven读取pom.xml时,它将立即用指定为的实际值替换占位符,如
${spring.version}

<properties>
  <spring.version>spring.version</spring.version>
</properties>

spring.version
因此,以后通过插件更改值太晚了!但是,如果此时没有可用的值,Maven将不会这样做。因此,您可以从pom.xml中删除这些属性,并让Maven插件在以后的构建生命周期中定义它们。这通常可以解决插件覆盖的属性的使用问题,但是

其次,可能还有另一个问题:Maven甚至在执行任何插件之前都可能会解决依赖项(包括它们的版本),这将阻止您完成这一切。如果是这种情况,您可以将属性移动到配置文件,而不是


3.0.4-发布
发展
3.0.5-发布
戳
3.0.1-发布

Hi Jeetendra,这是一个打字错误,我也犯了同样的错误,只是它给出了错误。请发布您的pom文件。因此,我们可以更好地分析它。我遵循同样的方法,得到了以下错误:'dependencies.dependency.version'for org.springframework:spring core:jar必须是有效的版本,但是是'${spring.version}'第44行第13栏我想这也不是问题。我们可以使用spring.version,它没有任何问题是的,我试图复制我这边的问题,但它有点混乱。解决它仍然有困难。嗨,Jeetendra,这是一个打字错误,我也做了同样的事情,只是它给出了错误。你可以发布你的pom文件。因此,我们可以更好地分析它。我遵循同样的方法,得到了以下错误:'dependencies.dependency.version'for org.springframework:spring core:jar必须是有效的版本,但是是'${spring.version}'第44行第13栏我想这也不是问题。我们可以使用spring.version,它没有任何问题是的,我试图复制我这边的问题,但它有点混乱。您永远不应该通过属性定义依赖项的版本,这些属性将被配置文件更改。如果您有不同的环境,那么您应该在每个环境中使用相同的依赖项。是的,虽然我不知道用例是什么,但根据环境使用不同的版本听起来确实很危险。您不应该定义