【问题标题】:Is there a way to read a text from a text.file and set it to maven property in pom.xml maven?有没有办法从 text.file 读取文本并将其设置为 pom.xml maven 中的 maven 属性?
【发布时间】:2020-03-22 12:54:03
【问题描述】:

有没有办法从 text.file 中读取文本并将其设置为 pom.xml maven 中的 maven 属性?我正在尝试使用以下代码。但是,我需要从 tmp.txt 读取文本并将其分配给 maven 属性“token”。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.5.0</version>
    <executions>
    <execution>
    <id>generate-random-string</id>
    <phase>install</phase>
    <goals>
    <goal>exec</goal>
    </goals>
    <configuration>
    <executable>bash</executable>
    <arguments>
    <argument>temp.sh</argument>
    </arguments>
    <workingDirectory>temp.txt</workingDirectory>
    </configuration>
    </execution>
    </executions>
</plugin>

temp.sh

PWD=${openssl rand hex 12}
echo $PWD >> temp.txt

【问题讨论】:

  • @AndyMan - 我正在努力从文本文件中读取文本并将其设置为 maven 属性。
  • 你可以试试@AndyMan 推荐的插件。将 bash 脚本更改为 PWD="$(ls -l)" echo "token = $PWD"&gt; temp.properties。然后读取该文件并使用令牌属性。见stackoverflow.com/questions/12619446/…
  • 我相信,它会起作用的。谢谢。
  • properties-maven-plugin 从此插件的配置文件中读取属性将受到限制,或者可以在整个文件中访问该属性?

标签: java maven maven-antrun-plugin maven-exec-plugin


【解决方案1】:

你可以使用properties-maven-plugin:https://www.mojohaus.org/properties-maven-plugin/

在你的 pom.xml 中。将生成文件的文件路径放在配置部分。

  <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>
                <!-- your generated file -->
                <file>temp.txt</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

【讨论】:

  • 这将如何处理只是一个值列表而不是键/值的文本文件?
  • 该文件是一个属性文件。它只是键值。对于列表,它应该是 listName=a,b,c,e... 您至少需要一个属性名称来引用它
猜你喜欢
  • 2013-09-05
  • 2016-07-13
  • 1970-01-01
  • 2013-10-19
  • 2013-06-20
  • 1970-01-01
  • 2019-04-11
  • 2023-03-25
  • 1970-01-01
相关资源
最近更新 更多