Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 577
ykesh Avatar asked Feb 04 '13 07:02

ykesh


People also ask

Where is Maven m2 settings xml?

The Maven settings file, settings. xml , is usually kept in the . m2 directory inside your home directory.

What is the use of settings xml in .m2 folder?

The settings element in the settings. xml file contains elements used to define values which configure Maven execution in various ways, like the pom. xml , but should not be bundled to any specific project, or distributed to an audience.


2 Answers

  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

like image 174
Manfred Moser Avatar answered Oct 12 '22 09:10

Manfred Moser


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.

like image 27
Peter Bratton Avatar answered Oct 12 '22 09:10

Peter Bratton