簡體   English   中英

如何從插件中讀取.m2 / settings.xml文件中的maven設置

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

三個問題按重要性遞減順序 - 鏈接將做。

  1. 我需要閱讀某些maven設置,例如我的maven插件中的代理,服務器 我如何從我的插件中讀取它們。 我可以從.m2 / settings.xml文件中讀取,但我認為必須有一種更簡單的方法(某些API已經完成了)。

  2. 我從開發者的食譜中看到有一堂課

      org.apache.maven.project.MavenProject 
    我需要什么依賴才能在我的插件中使用它 - 我覺得這樣會很好。

  3. 是否可以擁有自己的屬性

      的settings.xml 
    比如說

      <用戶> 
    <用戶>
    <用戶名> USER_NAME1 </用戶名>
    <密碼> encrypted_pa​​ssword </密碼>
    </用戶>
    </用戶>

    怎么樣 ?

PS:我是初學者。

更新1

通過Settings.xml注入POM屬性之后,我能夠創建和讀取自定義屬性。 但是我希望配置類似於貨物提供的配置。 例如

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

我如何實現這一目標。 對於我的第三個問題有一種解決方法,不確定它是否正確。

編輯

謝謝Jordan002 我知道我可以有多個配置文件,但我不知道使用它們。 這樣通過擁有配置文件,我可以設置我的變量的值,或者更確切地說通過說出類似的內容在我的插件中注入值

@Parameter(alias = "cargo.hostname")
private String hostname;
但是正如我所看到的,對於貨物插件而言,它所需的全部內容如下所示

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

類似地,或者可能不像這里沒有配置那樣相似

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

是我可以放置maven使用的代理信息的地方。 現在,我不想在某些配置文件中重新定義它,我不想解析此文件以獲取信息。

此外,我想做貨物正在做的事情。 它讓我可以在服務器和項目的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> 

貨物選擇我為tomcat7_local定義的配置 ,無需為此編寫配置文件。

  1. 按照此處所述注入設置組件http://maven.apache.org/plugin-tools/maven-plugin-tools-annotations/

  2. 它在Maven核心org.apache.maven:maven-core:3.0.5

  3. 直接使用屬性而不是嵌套。 例如http://maven.apache.org/examples/injecting-properties-via-settings.html

我對Cargo插件不太熟悉,但從文檔中看,它似乎可配置為任何其他Maven插件。 我將從你的'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>

並在運行時通過傳遞適當的配置文件指示您想要啟動/停止哪個tomcat:

mvn install -P tomcat6_local

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM