【问题标题】:How to read maven settings in .m2/settings.xml file from plugin如何从插件中读取 .m2/settings.xml 文件中的 Maven 设置
【发布时间】:2013-01-18 20:38:57
【问题描述】:

三个问题按重要性降序排列 - 链接即可。

  1. 我需要在我的 maven 插件中读取某些 maven 设置,例如 代理、服务器。我如何从我的插件中读取它们。我可以从 .m2/settings.xml 文件中读取,但我认为必须有更简单的方法(一些 API 已经做到了)。

  2. 我从developers cookbook看到有一个类

    org.apache.maven.project.MavenProject
    我需要什么依赖才能在我的插件中使用它 - 我觉得这很好。
  3. 是否可以在中拥有我自己的属性

    settings.xml
    比如说


    user_name1
    encrypted_pa​​ssword

    上一页>

    如何?

PS:我是初学者。

更新 1

我能够在Injecting POM Properties via Settings.xml 之后创建和读取自定义属性。但是我希望有类似于cargo 提供的配置。例如

    <servers>
        <server>
            <id>tomcat7_local</id>
            <configuration>
                <cargo.hostname>localhost</cargo.hostname>
                <cargo.remote.uri>http://localhost:8080/manager/text</cargo.remote.uri>
                <cargo.remote.username>my_username</cargo.remote.username>
                <cargo.remote.password>my_password</cargo.remote.password>
                <cargo.servlet.port>8080</cargo.servlet.port>
            </configuration>
        </server>
        <server>
            <id>tomcat6_local</id>
            <configuration>
                <cargo.hostname>localhost</cargo.hostname>
                <cargo.remote.uri>http://localhost:8080/manager</cargo.remote.uri>
                <cargo.remote.username>my_username</cargo.remote.username>
                <cargo.remote.password>my_password</cargo.remote.password>
                <cargo.servlet.port>8080</cargo.servlet.port>
            </configuration>
        </server>
    </servers>

我如何做到这一点。对我的第三个问题有一种解决方法,不确定它是否正确。

编辑

谢谢Jordan002!我知道我可以拥有多个配置文件,但我不知道如何使用它们。这样,通过配置文件,我可以设置变量的值,或者通过说类似

的内容将值注入到我的插件中
@Parameter(alias = "cargo.hostname")
private String hostname;
但正如我所见,对于货物插件,它所需要的所有定义如下
<servers>
  <server>
     <id>someId</id>
     <configuration>
     <!-- Configurations are placed here -->
     </configuration>
</servers>

类似,或者可能不那么类似,因为这里没有配置

<proxies>
  <proxy>
    <active>true</active>
    <protocol>http</protocol>
    <host>My_proxy_host</host>
    <port>My_proxy_port</port>
  </proxy>
</proxies>

是我可以放置 maven 使用的代理信息的地方。现在,我不想在某些 profiles 中重新定义它,也不想解析这个文件来获取信息。

此外,我想做货物正在做的事情。它让我在 servers 和项目的 pom 中编写所有配置,我只需要执行以下操作

<plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <configuration>
        <container>
            <containerId>tomcat7x</containerId>
            <type>remote</type>
        </container>
        <configuration>
            <type>runtime</type>
            <properties>
                <cargo.server.settings>tomcat7_local</cargo.server.settings>
            </properties>
        </configuration>
        <deployer>
            <type>remote</type>
        </deployer>
        <deployables>
            <deployable>
                <groupId>${project.groupId}</groupId>
                <artifactId>${project.artifactId}</artifactId>
                <type>war</type>
                <properties>
                    <context>${project.artifactId}</context>
                </properties>
            </deployable>
        </deployables>
    </configuration>
</plugin>

cargo 会获取我为 tomcat7_local 定义的配置,无需为此编写配置文件。

【问题讨论】:

    标签: java maven maven-plugin


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      我对 Cargo 插件不太熟悉,但从文档来看,它似乎可以像任何其他 Maven 插件一样进行配置。我将从您的“更新 1”中改变的是制作 tomcat6 和 tomcat7 配置文件:

      <profiles>
          <profile>
              <id>tomcat6_local</id>
              <activation>
                  <activeByDefault>false</activeByDefault>
              </activation>
              <properties>
                  <cargo.hostname>localhost</cargo.hostname>
                  <cargo.remote.uri>http://localhost:8080/manager/text</cargo.remote.uri>
                  <cargo.remote.username>my_username</cargo.remote.username>
                  <cargo.remote.password>my_password</cargo.remote.password>
                  <cargo.servlet.port>8080</cargo.servlet.port>
              </properties>
          </profile>
          <profile>
              <id>tomcat7_local</id>
              <activation>
                  <activeByDefault>false</activeByDefault>
              </activation>
              <properties>
                  <cargo.hostname>localhost</cargo.hostname>
                  <cargo.remote.uri>http://localhost:8080/manager</cargo.remote.uri>
                  <cargo.remote.username>my_username</cargo.remote.username>
                  <cargo.remote.password>my_password</cargo.remote.password>
                  <cargo.servlet.port>8080</cargo.servlet.port>
              </properties>
          </profile>
      </profiles>
      

      并在运行时通过传入适当的配置文件来指示您想要启动/停止哪个 tomcat:

      mvn install -P tomcat6_local
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2020-08-05
        • 2013-04-01
        • 2017-10-07
        • 1970-01-01
        • 2019-03-26
        • 2019-12-10
        • 2012-03-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多