[英]How to read maven settings in .m2/settings.xml file from plugin
Three questions in decreasing order of importance - Links will do. 三个问题按重要性递减顺序 - 链接将做。
I need to read certain maven settings such as proxies, servers in my maven plugin. 我需要阅读某些maven设置,例如我的maven插件中的代理,服务器 。 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).
我可以从.m2 / settings.xml文件中读取,但我认为必须有一种更简单的方法(某些API已经完成了)。
I see from developers cookbook there is a class 我从开发者的食谱中看到有一堂课
org.apache.maven.project.MavenProjectWhat dependency I need for this to be available in my plugin - I feel this would be good to have.org.apache.maven.project.MavenProject
Is it possible to have my own properties in 是否可以拥有自己的属性
settings.xmlsay for example的settings.xml
<users><用户>
<user><用户>
<username>user_name1</username><用户名> USER_NAME1 </用户名>
<password>encrypted_password</password><密码> encrypted_password </密码>
</user></用户>
</users></用户>
How ? 怎么样 ?
PS: I am a beginner. PS:我是初学者。
I was able to create and read custom properties following Injecting POM Properties via Settings.xml . 通过Settings.xml注入POM属性之后,我能够创建和读取自定义属性。 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.
对于我的第三个问题有一种解决方法,不确定它是否正确。
Thanks Jordan002 ! 谢谢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 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. 是我可以放置maven使用的代理信息的地方。 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
它让我可以在服务器和项目的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 tomcat7_local , no need to write a profile for this.
货物选择我为tomcat7_local定义的
配置 ,无需为此编写配置文件。
Inject the setttings component as described here http://maven.apache.org/plugin-tools/maven-plugin-tools-annotations/ 按照此处所述注入设置组件http://maven.apache.org/plugin-tools/maven-plugin-tools-annotations/
Its in Maven core org.apache.maven:maven-core:3.0.5 它在Maven核心org.apache.maven:maven-core:3.0.5
use properties directly and not nested. 直接使用属性而不是嵌套。 eg http://maven.apache.org/examples/injecting-properties-via-settings.html
例如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. 我对Cargo插件不太熟悉,但从文档中看,它似乎可配置为任何其他Maven插件。 What I would change from your 'Update 1' would be to make tomcat6 and tomcat7 profiles:
我将从你的'Update 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>
and indicate at run time which tomcat you would like to start/stop by passing in the appropriate profile: 并在运行时通过传递适当的配置文件指示您想要启动/停止哪个tomcat:
mvn install -P tomcat6_local
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.