简体   繁体   中英

How to read maven settings in .m2/settings.xml file from plugin

Three questions in decreasing order of importance - Links will do.

  1. I need to read certain maven settings such as proxies, servers in my maven plugin. How do I read them from my plugin. I can read from .m2/settings.xml file but I think there must be an easier way (some API that already does it).

  2. I see from developers cookbook there is a class

     org.apache.maven.project.MavenProject  
    What dependency I need for this to be available in my plugin - I feel this would be good to have.

  3. Is it possible to have my own properties in

     settings.xml  
    say for example

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

    How ?

PS: I am a beginner.

Update 1

I was able to create and read custom properties following Injecting POM Properties via Settings.xml . However I would like to have configuration similar to what cargo provides. Eg

    <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>

How do I achieve this. Have a kind of workaround for my 3rd problem not sure if its the right way.

Edit

Thanks Jordan002 ! I know I can have multiple profiles but I didn't know to use them. This way by having profiles I can set my variable's value or rather inject the value in my plugin by saying something like

@Parameter(alias = "cargo.hostname")
private String hostname;
But as I see, for cargo plugin all it requires is defined like below

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

Similarly, or may be not so similar as there is no here 那样相似

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

is where I can put proxy information that maven uses. Now, I don't want to redefine it inside some and I don't want to parse this file to get informations. 重新定义它,我不想解析此文件以获取信息。

Further, I would like do something like cargo is doing. It lets me write all the configuration inside and in project's pom I only have to do following 和项目的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> 

And cargo picks up configurations(s) that I defined for , no need to write a profile for this. 定义的 ,无需为此编写配置文件。

  1. Inject the setttings component as described here http://maven.apache.org/plugin-tools/maven-plugin-tools-annotations/

  2. Its in Maven core org.apache.maven:maven-core:3.0.5

  3. use properties directly and not nested. eg http://maven.apache.org/examples/injecting-properties-via-settings.html

I'm not too familiar with the Cargo plugin, but from the documentation, it appears to be configurable as any other Maven plugin would be. What I would change from your 'Update 1' would be to make tomcat6 and tomcat7 profiles:

<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>

and indicate at run time which tomcat you would like to start/stop by passing in the appropriate profile:

mvn install -P tomcat6_local

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM