Permalink
Browse files

added remove local artifact method, and ensured that local repo is used

  • Loading branch information...
1 parent 7d2323b commit cea62ba41308690131d244b780aecea52600f3f0 @hofmeister committed Feb 18, 2013
@@ -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>
@@ -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;
@@ -47,7 +48,7 @@
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);
@@ -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.
@@ -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);
}
/**
@@ -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);
}
/**
@@ -128,24 +139,44 @@ public Artifact resolveArtifact(String groupId,String artifactId,String type,Str
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();
@@ -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.
+ }
+ }
+ }
}
@@ -15,7 +15,7 @@
@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());
}
@@ -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();
@@ -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.