ykesh
ykesh

Reputation: 1209

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. E.g.

    <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

<servers>
  <server>
     <id>someId</id>
     <configuration>
     <!-- Configurations are placed here -->
     </configuration>
</servers>

Similarly, or may be not so similar as there is no configuration 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 profiles 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 servers and in project's pom I only have to do following

<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 tomcat7_local, no need to write a profile for this.

Upvotes: 15

Views: 7114

Answers (2)

Manfred Moser
Manfred Moser

Reputation: 29912

  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. e.g. http://maven.apache.org/examples/injecting-properties-via-settings.html

Upvotes: 4

Peter Bratton
Peter Bratton

Reputation: 6408

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.

Upvotes: 1

Related Questions