2

I have a maven project. I want to synchronize / download all dependencies to make my project to be resolved in this sense. I want to make it from java, using Maven CLI API.

There is a method that supposed to be same as "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);

}

But it fails with the following exception (no connector available to access repository central):

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)

It seems it does not use <repositories> I already have in my projects's pom.xml.

What is the way make it work?

ses
  • 13,174
  • 31
  • 123
  • 226
  • 1
    What about trying to set it in settings.xml. If you display the effective pom do you see those repositories? – ps-aux Aug 07 '13 at 22:17

2 Answers2

1

Ok. It seems I found the answer.

The answer were here:

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

And here:

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

Basically you should end up with this pom.xml (all about dependencies!):

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

Then, if you have an error like:

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

then make sure you use right version for your parent project. Parent should have the same version that real LOCAL root pom.xml has.

If you still have that error then the path to your parent like this:

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

This is actually default value. But in your case it might be

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

if you have deeper folder structure (like I had)


  • my client code is:

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

It creates: c:/.m2/repository folder. But it is empty for now.

Community
  • 1
  • 1
ses
  • 13,174
  • 31
  • 123
  • 226
1

Connector for remote http repositories is named wagon, configure your project like this:

<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>
MariuszS
  • 30,646
  • 12
  • 114
  • 155