Maven CLI - 同步项目依赖项

时间:2013-08-07 22:08:05

标签: java maven command-line-interface

我有一个maven项目。我想同步/下载所有依赖项以使我的项目在这个意义上得到解决。我想使用Maven CLI API从java中创建它。

有一种方法应该与“mvn clean validate”相同:

public static void syncProjectDeps(String projectPath) {

    MavenCli cli = new MavenCli();
    int result = cli.doMain(new String[]{"clean", "validate"}, projectPath, System.out, System.out);

    System.out.println(result);

}

但它失败并出现以下异常(没有可用于访问存储库中心的连接器):

INFO] Scanning for projects...
[ERROR]   The project com.mycompany.common:configuration:2.0.3-SNAPSHOT (C:\store\myproject\java\trunk\configuration\pom.xml) has 1 error
 ...
 No connector available to access repository central (http://repo.maven.apache.org/maven2)

我的项目<repositories>似乎没有使用pom.xml

使它运作的方式是什么?

2 个答案:

答案 0 :(得分:1)

确定。我似乎找到了答案。

答案在这里:

Can anyone give a good example of using org.apache.maven.cli.MavenCli programattically?

在这里:

https://groups.google.com/forum/#!topic/daisy-pipeline-dev/tubf3KXClM4

http://code.google.com/p/phloc-parent-pom/source/browse/trunk/pom.xml?r=47

基本上你最终应该使用这个pom.xml(所有关于依赖项的内容!):

<!-- maven api-->
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-embedder</artifactId>
            <version>3.0.5</version>
        </dependency>
        <dependency> <!-- https://stackoverflow.com/questions/4206679/can-anyone-give-a-good-example-of-using-org-apache-maven-cli-mavencli-programatt -->
            <groupId>org.sonatype.aether</groupId>
            <artifactId>aether-connector-wagon</artifactId>
            <version>1.13.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-http</artifactId> <!-- https://groups.google.com/forum/#!topic/daisy-pipeline-dev/tubf3KXClM4 -->
            <version>2.1</version>     <!-- http://code.google.com/p/phloc-parent-pom/source/browse/trunk/pom.xml?r=47 -->
        </dependency>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-utils</artifactId>
            <version>3.0.10</version>
        </dependency>

然后,如果您遇到如下错误:

Non-resolvable parent POM: Failure to find com.mycompany.common:commons:pom:2.0.6

然后确保为您的父项目使用正确的版本。父级应具有与实际LOCAL根pom.xml相同的版本。

如果您仍然遇到该错误,那么您父母的路径如下:

 <relativePath>../pom.xml</relativePath>

这实际上是默认值。但在你的情况下可能是

 <relativePath>../../pom.xml</relativePath>

如果你有更深层次的文件夹结构(就像我有的那样)


  • 我的客户代码是:

        System.setProperty("user.home", "C:\\my"); // ~/.m2/repository lives here    
        MavenCli cli = new MavenCli();
    
       // THIS IS IMPORTANT AS WELL  !!!
       int result = cli.doMain(
                    new String[]{"-DincludeScope=runtime", "dependency:copy-dependencies", "clean", "validate"},
                    projectPath,
                    System.out,
                    System.out);
    

创建:c:/.m2/repository文件夹。但现在它是空的。

答案 1 :(得分:1)

远程http存储库的连接器名为wagon,如下所示配置项目:

<dependencies>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-embedder</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.aether</groupId>
        <artifactId>aether-connector-wagon</artifactId>
        <version>0.9.0.M2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-http-lightweight</artifactId>
        <version>2.5</version>
    </dependency>
</dependencies>