【问题标题】:In Maven 3.5.2, how do I use a profile defined in my settings.xml file in my pom.xml file?在 Maven 3.5.2 中,如何在我的 pom.xml 文件中使用我的 settings.xml 文件中定义的配置文件?
【发布时间】:2017-11-28 22:02:49
【问题描述】:

假设我在 ~/.m2/settings.xml 文件中定义了以下配置文件

 <profiles>
   <profile>
     <id>artifactory</id>
     </repositories> 
        <repository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>thirdparty</id>
          <name>scscm-thirdparty</name>
          <url>http://www.mycompany.com/artifactory/my-thirdparty-repo</url>
        </repository>
      </repositories>
    </profile>
  </profiles>

现在我想从 settings.xml 中定义的第三方存储库 id 下载自制的 apr-1.6.2.tar.gz 制品,所以在我的 pom.xml 文件中有

<artifactId>apr</artifactId>
<groupId>com.company</groupId>
<version>1.6.2</version>
<packaging>pom</packaging>

<dependencies>
  <dependency>
    <groupId>org.apache</groupId>
    <artifactId>apr</artifactId>
    <version>1.6.2</version>
    <type>tar.gz</type>
  </dependency>
</dependencies>

<build>
  <plugins>
    <plugin>
    <groupId>com.googlecode.maven-download-plugin</groupId>
    <artifactId>download-maven-plugin</artifactId>
    <version>1.4.0</version>
    <executions>
      <execution>
        <id>apr</id>
        <phase>pre-integration-test</phase>
          <goals>
            <goal>wget</goal>
          </goals>
          <configuration>
            # HOW DO I SPECIFY URL FROM PROFILE HERE????
           <unpack>false</unpack>
         </configuration>
      </execution>
    </executions>
    </plugin>
  </plujgins>
</build>

我在网上看到了配置文件的示例,但它们都是在 pom.xml 本身中定义的,但这不是我想要做的。我只想在我的 pom.xml 文件中使用我的 settings.xml 配置文件中定义的 URL。

【问题讨论】:

  • 在你的 settings.xml 中定义一个属性并使用它。但我认为这不是一个好主意...此外,我会问您为什么要单独下载文件...
  • 你说得对,我想要的不是房产,而是个人资料。我正在下载此文件,以便编译、构建并将其打包成 RPM。
  • 如果您使用的是 apr,它本身应该可以通过 RPM 依赖项获得?不是吗?除此之外,我不确定在这种情况下使用 Maven 是否真的是一个好主意......
  • apr 只是一个例子。它可以是任何文件。

标签: maven maven-3 maven-plugin


【解决方案1】:

您可以通过多种方式实现这一目标

  1. 在 Settings.xml 中设置活动配置文件

    <profiles>
     <profile>
       <id>artifactory</id>
       </repositories> 
          <repository>
            <snapshots>
              <enabled>false</enabled>
            </snapshots>
            <id>thirdparty</id>
            <name>scscm-thirdparty</name>
            <url>http://www.mycompany.com/artifactory/my-thirdparty-repo</url>
          </repository>
        </repositories>
      </profile>
    </profiles>
    
    <activeProfiles>
      <activeProfile>artifactory</activeProfile>
    </activeProfiles>
    
  2. 从 CLI 设置活动配置文件

    mvn clean verify -P artifactory

  3. 在 Settings.xml 中将活动配置文件设置为默认值

    <profiles>
     <profile>
       <id>artifactory</id>
       </repositories> 
          <repository>
            <snapshots>
              <enabled>false</enabled>
            </snapshots>
            <id>thirdparty</id>
            <name>scscm-thirdparty</name>
            <url>http://www.mycompany.com/artifactory/my-thirdparty-repo</url>
          </repository>
        </repositories>
        <activation>
          <activeByDefault>true</activeByDefault>
        </activation>
      </profile>
    </profiles>
    

有关这些选项的更多信息,请查看maven 页面

【讨论】:

    【解决方案2】:

    通过命令行 (-P &lt;name&gt;) 或使用您喜欢的触发器激活它们。但问问自己,这是否是找到您的个人资料的正确位置。有关更多信息,请查看文档:

    1.Maven Doc

    1. same question?

    2. Profile activation in Eclipse

    【讨论】:

    • 所提供的链接都没有回答我提出的具体问题。比如,我在哪里询问过 Eclipse?
    • hm 我认为您的问题是关于在您的构建中激活配置文件,这就是原因......
    猜你喜欢
    • 2019-03-26
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2017-04-03
    相关资源
    最近更新 更多