2011-06-09 118 views
6

我有我们的项目所必需的第三方jar。它在中央maven仓库中不可用,所以我使用maven-install-plugin在构建期间在本地安装jar。我将“安装文件”目标与“验证”阶段绑定在一起,而这主要起作用。 pom.xml文件摘录低于:mvn清洁没有依赖关系

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
... 
<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-install-plugin</artifactId> 
      <version>2.3</version> 
      <executions> 
       <execution> 
        <id>install-myartifact</id> 
        <phase>validate</phase> 
        <goals> 
         <goal>install-file</goal> 
        </goals> 
        <configuration> 
         <file>${basedir}/lib/myartifact-1.2.3.jar</file> 
         <groupId>com.example</groupId> 
         <artifactId>myartifact</artifactId> 
         <version>1.2.3</version> 
         <packaging>jar</packaging> 
         <generatePom>true</generatePom> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 
<dependencies> 
    <dependency> 
     <groupId>com.example</groupId> 
     <artifactId>myartifact</artifactId> 
     <version>1.2.3</version> 
    </dependency> 
</dependencies> 

但是,这里有一个问题。我们的大多数开发人员和我们的Jenkins安装都运行“mvn clean install”。 “验证”阶段不是“干净”生命周期的一部分,干净地莫名其妙地需要所有依赖关系存在才能运行。所以有人第一次运行这个版本,它不起作用。

[INFO] Scanning for projects... 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building MyModule 
[INFO] task-segment: [clean, install] 
[INFO] ------------------------------------------------------------------------ 
[INFO] [clean:clean] 
[INFO] Deleting directory C:\svn\trunk\mymodule\target 
Downloading: http://nexusserver.local:8080/nexus/content/groups/public/com/example/myartifact-1.2.3.pom 
[INFO] Unable to find resource 'com.example:myartifact:pom:1.2.3' in repository central (http://central) 
[INFO] ------------------------------------------------------------------------ 
[ERROR] BUILD ERROR 
[INFO] ------------------------------------------------------------------------ 
[INFO] Failed to resolve artifact. 

Missing: 
---------- 
1) com.example:myartifact:jar:1.2.3 

    Try downloading the file manually from the project website. 

    Then, install it using the command: 
     mvn install:install-file -DgroupId=com.example -DartifactId=myartifact -Dversion=1.2.3 -Dpackaging=jar -Dfile=/path/to/file 

    Alternatively, if you host your own repository you can deploy the file there: 
     mvn deploy:deploy-file -DgroupId=com.example -DartifactId=myartifact -Dversion=1.2.3 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id] 

    Path to dependency: 
    1) com.example:mymodule:war:0.0.1-SNAPSHOT 
    2) com.example:myartifact:jar:1.2.3 

---------- 
1 required artifact is missing. 

for artifact: 
    com.example:mymodule:war:0.0.1-SNAPSHOT 

from the specified remote repositories: 
    nexus (http://nexusserver.local:8080/nexus/content/groups/public) 



[INFO] ------------------------------------------------------------------------ 
[INFO] For more information, run Maven with the -e switch 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 1 second 
[INFO] Finished at: Thu Jun 09 11:01:24 EDT 2011 
[INFO] Final Memory: 17M/247M 
[INFO] ------------------------------------------------------------------------ 

如果我是运行简单的“MVN安装”,罐子是在安装了“验证”,并且我可以运行“MVN净安装”,在随后的版本。但是,我们的构建服务器没有这种灵活性。我已考虑以下内容:

  1. 将阶段转到“预清理”,但假设每个人都始终使用清洁。如果有人只是简单地运行“mvn install”,这将无济于事。
  2. 复制执行过程,其中一个在“预清理”期间发生,另一个在“验证”期间发生。这涵盖了所有的基础,但复制的代码留下了不好的口味。

理想情况下,我想要其他选项。有没有依赖关系可以运行干净?或者运行插件两次而不必完全复制执行?谢谢!

回答

5

它看起来像你正在使用nexus。将工件部署到联合回购可能更容易,而不是必须通过此项目来维护工件。

+1

这是一个很好的想法,但我们正处于一个分散的开发团队的国际公司。在主连线上获得它是一个漫长的过程。 =( – 2011-06-09 15:58:53

+0

如何使用本地文件库?您可以将文件打包到您的模块的peer/child目录中,然后将该目录作为文件:repo添加到您的pom.xml文件中。您可以配置settings.xml以使用你的nexus回购作为镜像,但仍然允许本地文件回收 – massfords 2011-06-10 14:10:46

+0

Massfords:更改settings.xml是一个非启动器,它会影响每个使用该构建的人,我最好是在主连接上获得工件。我会接受这个答案,因为即使它不能解决我的特殊情况,它应该适用于大多数人。 – 2011-06-13 19:27:41

0

这是未经测试的,但您是否可以忽略clean插件配置中的错误?如:

<plugin> 
    <artifactId>maven-clean-plugin</artifactId> 
    <version>2.4.1</version> 
    <configuration> 
    <failOnError>false</failOnError> 
    </configuration> 
</plugin> 

(这是来自Maven Clean Plugin : Ignoring Clean Errors

+1

不幸的是,不,failOnError不会绕过无法解决依赖关系。 – 2011-06-09 16:01:48

0

下面是一个更可能性。

配置maven跳过清理阶段并在初始化期间运行clean。虽然没有尝试过。

这个的缺点是maven会一直清理输出文件夹。

5

我遇到了一个相关的问题,我发现这个问题对谷歌搜索解决方案时,所以在这里我要指出的:在一个多模块项目

MVN清洁失败时,有内缺少依赖同一个项目,如果在清理过程中调用插件。

我们在某些模块的清理阶段调用antrun-plugin,并且因为所有的依赖需要存在于maven仓库中,包括其他模块在同一个反应器中,在某些情况下还没有还没建好(比如你刚刚碰到了项目版本,或者你正在开始一个新项目)。

这是一个在http://jira.codehaus.org/browse/MANTRUN-78中报告的maven-antrun错误 - 这再次导致maven内核中的错误:http://jira.codehaus.org/browse/MNG-3283

我的解决方法是为开发人员(和詹金斯)提供另一种干净的方法(shell/bat脚本,ant脚本或一些git/hg clean操作),并让他们调用它。

我会为你的团队建议一个类似的解决方法(或者只是在你的团队内部设置一个共享的maven仓库,如果需要的话可以使用其中一个开发者机器)。