【问题标题】:how to access variable in pom.xml from external properties file?如何从外部属性文件访问 pom.xml 中的变量?
【发布时间】:2017-06-25 17:50:40
【问题描述】:

我需要通过pom.xml 中的键从外部属性文件中获取值。属性文件的位置在src/main/resources/dev.properties。 我试图通过使用 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Simple_project</groupId>
    <artifactId>Project</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>Project Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>${Project.Name}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <outputDirectory>${path}</outputDirectory>
            </configuration>
        </plugin>
    </plugins>
    </build>

</project>

// properties File

dev.properties:

projectPath=/home/user/warfile
Project.Name = newProject

Thanks & Regards,
Tharanya B

【问题讨论】:

    标签: maven


    【解决方案1】:

    你必须使用属性插件:

    插件

    <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}/${propertiesFile}</file>
                  </files>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
    

    Mvn 命令

    mvn -DpropertiesFile=dev.properties properties:read-project-properties clean install
    

    或者

    您可以对文件进行硬编码:

    <file>${basedir}/dev.properties</file>
    

    【讨论】:

    • 上面提到的mvn命令不是在dev.properties文件中提到的路径中创建war文件,而是在项目的目标文件夹中创建war文件。
    • btw.. 我认为你应该使用 ${projectPath}.. 而不是 ${path}
    • ${Project.Name} 不起作用。我已更改为 ${projectPath}
    • 在 pom.xml 中定义 ${Project.Name} 时,在 pom.xml 中显示以下错误。目标 org.apache.maven.plugins:maven-war-plugin 的参数 'warName' :2.2:war 丢失或无效
    • 尝试明确定义文件 ..dev.properties。或 .${basedir}/dev.properties
    猜你喜欢
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 2013-01-23
    • 2021-10-23
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多