简体   繁体   English

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

[英]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. 我需要阅读某些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已经完成了)。

  2. I see from developers cookbook there is a class 我从开发者的食谱中看到有一堂课

     org.apache.maven.project.MavenProject 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 的settings.xml 
    say for example 比如说

     <users> <用户> 
    <user> <用户>
    <username>user_name1</username> <用户名> USER_NAME1 </用户名>
    <password>encrypted_password</password> <密码> encrypted_pa​​ssword </密码>
    </user> </用户>
    </users> </用户>

    How ? 怎么样 ?

PS: I am a beginner. PS:我是初学者。

Update 1 更新1

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. 对于我的第三个问题有一种解决方法,不确定它是否正确。

Edit 编辑

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定义的配置 ,无需为此编写配置文件。

  1. 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/

  2. Its in Maven core org.apache.maven:maven-core:3.0.5 它在Maven核心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 例如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.

相关问题 如何在 maven ~/.m2/settings.xml 中设置 jdk 11 - how to set jdk 11 in maven ~/.m2/settings.xml 更改maven插件中的settings.xml - Alter settings.xml from a maven plugin Eclipse插件:如何读取在M2Eclipse插件中配置的“ settings.xml”文件路径 - Eclipse plugin: how to read the “settings.xml” file path which is configured in M2Eclipse plugin 如何从 mavens.m2 文件夹中排除读取 settings.xml - How can I exclude reading settings.xml from mavens .m2 folder 配置Maven scm插件以使用settings.xml中的serverID - Configure Maven scm plugin to use serverID from settings.xml 如何在settings.xml中的配置文件中使用Maven插件 - How to use maven plugin in profile in settings.xml 如何在settings.xml中使用artifactory-maven-plugin中的用户名和密码? - How use username and password in artifactory-maven-plugin from settings.xml? artifactory-maven-plugin:如何在不使用settings.xml的情况下解决来自私有Artifactory的依赖关系 - artifactory-maven-plugin: How to resolve dependencies from private Artifactory without settings.xml 什么是maven settings.xml文件以及它与pom.xml的不同之处 - What is a maven settings.xml file and how it is different from pom.xml 如何在Maven的settings.xml中配置无代理? - how to configure no proxy in settings.xml in maven?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM