如何从插件中读取.m2/settings.xml文件中的maven设置

yke*_*esh 15 java maven-plugin maven

三个问题按重要性递减顺序 - 链接将做.

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

  2. 我从开发者的食谱中看到有一堂课

    org.apache.maven.project.MavenProject
    我需要什么依赖才能在我的插件中使用它 - 我觉得这样会很好.

  3. 是否可以拥有自己的属性

    settings.xml
    比如说

    <users>
    <user>
    <username>user_name1</username>
    <password>encrypted_password</password>
    </user>
    </users>

    怎么样 ?

PS:我是初学者.

更新1

通过Settings.xml注入POM属性之后,我能够创建和读取自定义属性.但是我希望配置类似于货物提供的配置.例如

    <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>
Run Code Online (Sandbox Code Playgroud)

我如何实现这一目标.对于我的第三个问题有一种解决方法,不确定它是否正确.

编辑

谢谢Jordan002!我知道我可以有多个配置文件,但我不知道使用它们.这样通过拥有配置文件,我可以设置我的变量的值,或者更确切地说通过说出类似的东西来在我的插件中注入值

@Parameter(alias = "cargo.hostname")
private String hostname;
但是正如我所看到的,对于货物插件而言,它所需的全部内容如下所示

<servers>
  <server>
     <id>someId</id>
     <configuration>
     <!-- Configurations are placed here -->
     </configuration>
</servers>
Run Code Online (Sandbox Code Playgroud)

类似地,或者可能不像这里没有配置那样相似

<proxies>
  <proxy>
    <active>true</active>
    <protocol>http</protocol>
    <host>My_proxy_host</host>
    <port>My_proxy_port</port>
  </proxy>
</proxies>
Run Code Online (Sandbox Code Playgroud)

是我可以放置maven使用的代理信息的地方.现在,我不想在某些配置文件中重新定义它,我不想解析此文件以获取信息.

此外,我想做货物正在做的事情.它让我可以在服务器和项目的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>
Run Code Online (Sandbox Code Playgroud)

货物选择我为tomcat7_local定义的配置,无需为此编写配置文件.

Man*_*ser 4

  1. 按照此处所述注入设置组件http://maven.apache.org/plugin-tools/maven-plugin-tools-annotations/

  2. 它在 Maven 核心 org.apache.maven:maven-core:3.0.5 中

  3. 直接使用属性而不是嵌套。例如http://maven.apache.org/examples/injecting-properties-via-settings.html