【问题标题】:How to write in xml file using maven?如何使用maven写入xml文件?
【发布时间】:2016-08-19 13:39:57
【问题描述】:

我有一种情况,我必须在预定义的 xml 文件中写入项目依赖项之一的工件名称,该文件将与项目 jar 一起保存并驻留在项目的资源文件夹中。 例如 预定义.xml -

<project>
    <projectName>somename</projectName>
    <projectjar>
        <name>some.jar</name>
    </projectjar>
    <dependantJarsInfo>
        <folderName>dependant_jars</folderName>
        <dependantJars>
            <name>dependency.jar</name>
        </dependantJars>
    </dependantJarsInfo>    
</plugin> 

pom.xml

<project>
    <groupId>com.mycom</groupId>
    <artifactId>someproject</artifactId>
    <packaging>jar</packaging>
    <version>5.8.0.0</version>

    <dependencies>

        <dependency>
            <groupId>com.mycom.</groupId>
            <artifactId>dependency</artifactId>
            <version>LATEST</version>
        </dependency>

    </dependencies>

</project>

我需要获取像dependency-1.2.0这样的全名,其中LATEST=1.2.0,并在指定标签处写入给定的predefined.xml文件。

        <dependantJars>
            <name>dependency.jar</name>
        </dependantJars>

如果是的话,maven 是否有可能,那么如何?如果没有,那么我应该采取什么替代方法来解决这个问题。

【问题讨论】:

  • 将依赖版本写入 pom 的确切问题是什么?您想用这种方法解决什么问题?
  • khmarbaise 感谢您的回复,我们正在推动我们的构建系统继续集成一个全自动过程,其中使用最新的关键字 maven 本身找到依赖项的最后一个构建版本,这就是为什么我们不编写版本pom中的任何家庭依赖,我希望你明白我的意思。

标签: xml maven


【解决方案1】:

在对这个问题进行研究后,我得到了解决方案。 我使用两个maven插件来解决它。

  1. maven 依赖插件
  2. gmaven 插件

这个插件的使用 -

 <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version>
<executions>
    <execution>
        <id>build-classpath</id>
        <phase>validate</phase>
        <goals>
            <goal>build-classpath</goal>
        </goals>
        <configuration>
        <outputFile>target/classPath.txt</outputFile>
            <includeArtifactIds>
                dependency
            </includeArtifactIds>
        </configuration>
    </execution>
</executions>
</plugin>   

<plugin>
    <groupId>org.codehaus.groovy.maven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <executions>
        <execution>
            <id>groovy-magic</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>

                <source>
                    def theInfoName = "target/classPath.txt";
                    File theInfoFile = new File(theInfoName)
                    def words
                    def key
                    def value
                    if (!theInfoFile.exists()) {
                    println "File does not exist"
                    } else {
                    theInfoFile.eachLine { line ->
                    if (line.trim().size() == 0) {
                    return null
                    } else {
                    words = line
                    File theInfoFileto = new File(words)
                    ant.replace(file: "target/classes/myPackagepath/predefind.xml", token: "dependency.jar", value: theInfoFileto.getName())
                    }
                    }
}                                
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

希望这对某人有所帮助。

【讨论】:

    【解决方案2】:

    我认为你实际上有错误的方法来解决你的问题。您应该尝试改用Maven Properties

    示例

    您在 pom.xml 中定义页面名称:

    <project>
    
      <properties>
        <package.name>dependency.jar</package.name>
      </properties>
    
    </project>
    

    然后,当您想使用命令行更改包的名称时,可以使用:

    mvn -Dpackage.name=othername package ...
    

    但是如果你不覆盖你的包名,你仍然可以使用你pom.xml中定义的默认值

    mvn package ...
    

    关于此的其他 SO 问题:

    【讨论】:

    • Mickaël B 感谢您的回答,但问题是不要在 pom.xml 中获取或更新任何东西,我只需要在运行时从 maven 获取依赖项详细信息并注入到我的预定义.xml 文件中。
    猜你喜欢
    • 2011-09-06
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 2010-12-27
    • 2014-03-12
    相关资源
    最近更新 更多