Skip to content

Commit

Permalink
added remove local artifact method, and ensured that local repo is used
Browse files Browse the repository at this point in the history
  • Loading branch information
hofmeister committed Feb 18, 2013
1 parent 7d2323b commit cea62ba
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
6 changes: 5 additions & 1 deletion modules/webi-maven/pom.xml
Expand Up @@ -71,7 +71,11 @@
<artifactId>maven-settings</artifactId>
<version>2.0.6</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>


</dependencies>
Expand Down
57 changes: 50 additions & 7 deletions modules/webi-maven/src/main/java/com/vonhof/webi/maven/Maven.java
@@ -1,6 +1,7 @@
package com.vonhof.webi.maven;


import org.apache.commons.io.FileUtils;
import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader;
import org.apache.maven.repository.internal.DefaultVersionRangeResolver;
import org.apache.maven.repository.internal.DefaultVersionResolver;
Expand Down Expand Up @@ -47,7 +48,7 @@ public class Maven {
public Maven() {
String userHome = System.getProperty("user.home");

localRepositoryPath = userHome+"/.m2/repository/";
localRepositoryPath = userHome+"/.m2/repository";

locator.addService(ArtifactDescriptorReader.class, DefaultArtifactDescriptorReader.class);
locator.addService(VersionResolver.class, DefaultVersionResolver.class);
Expand All @@ -63,6 +64,9 @@ public Maven() {
addRepository("central","http://repo.maven.apache.org/maven2/",true,false);
}

public Artifact resolveArtifact(String groupId,String artifactId,String version) throws ArtifactResolutionException, DependencyCollectionException {
return resolveArtifact(groupId, artifactId, version,false);
}

/**
* Resolve artifact.
Expand All @@ -72,12 +76,18 @@ public Maven() {
* @param groupId
* @param artifactId
* @param version
* @param forceRemote
* @return
* @throws ArtifactResolutionException
* @throws DependencyCollectionException
*/
public Artifact resolveArtifact(String groupId,String artifactId,String version) throws ArtifactResolutionException, DependencyCollectionException {
return resolveArtifact(String.format("%s:%s:%s",groupId,artifactId,version));
public Artifact resolveArtifact(String groupId,String artifactId,String version,boolean forceRemote) throws ArtifactResolutionException, DependencyCollectionException {
return resolveArtifact(String.format("%s:%s:%s",groupId,artifactId,version),forceRemote);
}


public Artifact resolveArtifact(String groupId,String artifactId,String type,String version) throws ArtifactResolutionException, DependencyCollectionException {
return resolveArtifact(groupId, artifactId, type, version,false);
}

/**
Expand All @@ -89,12 +99,13 @@ public Artifact resolveArtifact(String groupId,String artifactId,String version)
* @param artifactId
* @param type
* @param version
* @param forceRemote
* @return
* @throws ArtifactResolutionException
* @throws DependencyCollectionException
*/
public Artifact resolveArtifact(String groupId,String artifactId,String type,String version) throws ArtifactResolutionException, DependencyCollectionException {
return resolveArtifact(String.format("%s:%s:%s:%s", groupId, artifactId, type, version));
public Artifact resolveArtifact(String groupId,String artifactId,String type,String version,boolean forceRemote) throws ArtifactResolutionException, DependencyCollectionException {
return resolveArtifact(String.format("%s:%s:%s:%s", groupId, artifactId, type, version),forceRemote);
}

/**
Expand Down Expand Up @@ -128,24 +139,44 @@ public List<Artifact> resolveDependencies(String artifactId) throws DependencyCo
return nlg.getArtifacts(true);
}

public Artifact resolveArtifact(String artifactId) throws DependencyCollectionException, ArtifactResolutionException {
return resolveArtifact(artifactId,false);
}

/**
* Resolve artifact by id. Downloads artifact from remote repository if needed.
* Returned artifact instance has contains file pointer to locale file.
*
* @param artifactId
* @param forceRemote Ignore local repo
* @return
* @throws ArtifactResolutionException
* @throws DependencyCollectionException
*/
public Artifact resolveArtifact(String artifactId) throws ArtifactResolutionException, DependencyCollectionException {
public Artifact resolveArtifact(String artifactId,boolean forceRemote) throws ArtifactResolutionException, DependencyCollectionException {

Artifact artifact = new DefaultArtifact(artifactId);
Dependency dependency = new Dependency( artifact, "compile" );


if (!forceRemote) {
LocalArtifactRequest localArtifactRequest = new LocalArtifactRequest();
localArtifactRequest.setArtifact(artifact);
localArtifactRequest.setRepositories(getRemoteRepositories());
final LocalArtifactResult localArtifactResult = localRepoManager.find(session(), localArtifactRequest);

if (localArtifactResult.getFile() != null
&& localArtifactResult.getFile().exists()) {
return artifact.setFile(localArtifactResult.getFile());
}
}



//Resolve artifact
ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.setArtifact(artifact);
artifactRequest.setRepositories(getRemoteRepositories());

final ArtifactResult result = repositorySystem.resolveArtifact(session(), artifactRequest);

return result.getArtifact();
Expand Down Expand Up @@ -194,4 +225,16 @@ protected RepositorySystemSession session() {

return session;
}

public void removeLocalArtifact(String groupId, String artifactId, String version) {
final String jarPath = localRepoManager.getPathForLocalArtifact(new DefaultArtifact(groupId, artifactId, "jar", version));
final File jarFile = new File(localRepositoryPath+File.separator+jarPath);
if (jarFile.exists()) {
try {
FileUtils.deleteDirectory(jarFile.getParentFile());
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
}
Expand Up @@ -15,7 +15,7 @@ public class MavenTest {
@Test
public void testResolveArtifact() throws Exception {
Maven mvn = new Maven();
final Artifact artifact = mvn.resolveArtifact("com.hazelcast:hazelcast:2.5");
final Artifact artifact = mvn.resolveArtifact("com.hazelcast:hazelcast:2.5",true);

assertTrue("Got a jar file that exists",artifact.getFile().exists());
}
Expand All @@ -35,23 +35,40 @@ public void testResolveDependencies() throws Exception {
public void testAddRepository() throws Exception {
Maven mvn = new Maven();

mvn.removeLocalArtifact("com.caucho", "resin-hessian", "3.2.1");

boolean gotException = false;
try {
mvn.resolveArtifact("com.caucho", "resin-hessian", "3.2.1");
mvn.resolveArtifact("com.caucho", "resin-hessian", "3.2.1",true);

} catch(ArtifactResolutionException ex) {
gotException = true;
}

assertTrue("Artifact could not be resolved from maven central",gotException);

//Add additional maven repo.
mvn.addRepository("caucho","http://caucho.com/m2");

final Artifact artifact = mvn.resolveArtifact("com.caucho", "resin-hessian", "3.2.1");
final Artifact artifact = mvn.resolveArtifact("com.caucho", "resin-hessian", "3.2.1",true);

assertTrue("Got jar file that doesn't exist in Maven Central repo", artifact.getFile().exists());
}

@Test
public void testLocalRepository() throws Exception {
Maven mvn = new Maven();
mvn.addRepository("caucho","http://caucho.com/m2");

mvn.resolveArtifact("com.caucho", "resin-hessian", "3.2.1");

//Init new maven instance (without the extra repo)
mvn = new Maven();
Artifact artifact = mvn.resolveArtifact("com.caucho", "resin-hessian", "3.2.1");

assertTrue("Got jar file that doesn't exist in Maven Central repo from local repo",artifact.getFile().exists());
}

@Test
public void testAddArtifact() throws Exception {
Maven mvn = new Maven();
Expand Down
1 change: 1 addition & 0 deletions modules/webi-maven/webi-maven.iml
Expand Up @@ -42,6 +42,7 @@
<orderEntry type="library" name="Maven: org.apache.maven:maven-settings:2.0.6" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.plexus:plexus-container-default:1.0-alpha-9-stable-1" level="project" />
<orderEntry type="library" name="Maven: classworlds:classworlds:1.1-alpha-2" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-io:1.3.2" level="project" />
</component>
</module>

0 comments on commit cea62ba

Please sign in to comment.