在Maven中将所需的Jar文件和可选的第二个jar文件安装到本地存储库中

时间:2011-12-24 04:59:27

标签: maven

我的产品在其发行版中包含一个Jar文件,我创建了一个pom.xml,它使用install-file mojo将Jar安装到本地存储库中。所以用户解压缩我的zip文件并键入“mvn install”,一切正常。

我的问题是我有第二个Jar文件,我也想使用相同的pom.xml安装,但是这个Jar文件是可选的,可能存在也可能不存在(Jar文件由用户单独下载,放在同一目录中)。我已经尝试过install-file和build-helper:attach-artifact,并且无法弄清楚如何在单个POM中执行此操作。我很高兴让用户输入一些不同的命令来安装这个Jar文件,或者让它与“mvn install”一起工作。

1 个答案:

答案 0 :(得分:1)

一种可能性是使用profile,它会根据第二个jar的存在而被激活。此配置文件可用于使用您在上面提到的build helper maven plugin目标附加其他工件。像这样......

<project>
  ...
    <profiles>
        <profile>
          <id>second-jar</id>
          <activation>
             <file>
              <exists>${basedir}/location/of/second.jar</exists>          
             </file>
          </activation>
          <build>
            <plugins>
              ...
              <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                  <execution>
                    <id>attach-artifacts</id>
                    <phase>package</phase>
                    <goals>
                      <goal>attach-artifact</goal>
                    </goals>
                    <configuration>
                      <artifacts>
                        <artifact>
                          <file>second</file>
                          <type>jar</type>
                        </artifact>
                      </artifacts>
                    </configuration>
                  </execution>
               </executions>
             </plugin>
          </plugins>
       </build>
     </profile>
   </profiles>
</project>
相关问题