Skip to content

Commit

Permalink
Fixed Forge Addon dependency resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Mar 1, 2013
1 parent 53900e4 commit 8056c3a
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
/**
* When an addon is installed, another addons could be required. This object returns the necessary information for the
* installation of an addon to succeed, like required addons and dependencies
*
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*
*/
public class InstallRequestImpl implements InstallRequest
{
Expand All @@ -47,7 +47,7 @@ public class InstallRequestImpl implements InstallRequest

/**
* Package-access constructor. Only AddonManager should be allowed to call this constructor.
*
*
* @param addonManager
*/
InstallRequestImpl(AddonManager addonManager, AddonRepository repository, DependencyNode requestedAddonNode)
Expand Down Expand Up @@ -80,7 +80,7 @@ public class InstallRequestImpl implements InstallRequest

/*
* (non-Javadoc)
*
*
* @see org.jboss.forge.addon.manager.impl.InstallRequest#getRequestedAddon()
*/
@Override
Expand All @@ -91,7 +91,7 @@ public DependencyNode getRequestedAddon()

/*
* (non-Javadoc)
*
*
* @see org.jboss.forge.addon.manager.impl.InstallRequest#getOptionalAddons()
*/
@Override
Expand All @@ -102,7 +102,7 @@ public List<DependencyNode> getOptionalAddons()

/*
* (non-Javadoc)
*
*
* @see org.jboss.forge.addon.manager.impl.InstallRequest#getRequiredAddons()
*/
@Override
Expand All @@ -113,27 +113,30 @@ public List<DependencyNode> getRequiredAddons()

/*
* (non-Javadoc)
*
*
* @see org.jboss.forge.addon.manager.impl.InstallRequest#perform()
*/
@Override
public void perform()
{
List<AddonId> addonsToEnable = new ArrayList<AddonId>();
for (DependencyNode requiredAddon : getRequiredAddons())
{
AddonId requiredAddonId = toAddonId(requiredAddon);
if (repository.isDeployed(requiredAddonId))
{
log.info("Addon " + requiredAddonId + " is already deployed. Skipping...");
}
else
{
addonManager.install(requiredAddonId).perform();
}
deploy(requiredAddonId, requiredAddon);
addonsToEnable.add(requiredAddonId);
}

AddonId requestedAddonId = toAddonId(requestedAddonNode);
deploy(requestedAddonId, requestedAddonNode);
for (AddonId addonId : addonsToEnable)
{
repository.enable(addonId);
}
repository.enable(requestedAddonId);
}

Expand Down
10 changes: 8 additions & 2 deletions maven/impl/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<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/xsd/maven-4.0.0.xsd">
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jboss.forge</groupId>
Expand Down Expand Up @@ -44,12 +45,17 @@
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jboss.forge</groupId>
<artifactId>example</artifactId>
<version>2.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-http</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import org.apache.maven.settings.building.SettingsBuildingException;
import org.apache.maven.settings.building.SettingsBuildingRequest;
import org.apache.maven.settings.building.SettingsBuildingResult;
import org.apache.maven.wagon.Wagon;
import org.apache.maven.wagon.providers.http.HttpWagon;
import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.connector.wagon.WagonProvider;
import org.sonatype.aether.connector.wagon.WagonRepositoryConnectorFactory;
import org.sonatype.aether.impl.internal.DefaultServiceLocator;
import org.sonatype.aether.repository.Authentication;
Expand All @@ -33,7 +36,7 @@

/**
* Configures the Maven API for usage inside Forge
*
*
* TODO: Remove in the future, use the ShrinkWrap Descriptors API ?
*/
public class MavenContainer
Expand Down Expand Up @@ -95,8 +98,26 @@ public RepositorySystem getRepositorySystem()

final DefaultServiceLocator locator = new MavenServiceLocator();
locator.setServices(ModelBuilder.class, new DefaultModelBuilderFactory().newInstance());
// Installing Wagon to fetch from HTTP repositories
locator.setServices(WagonProvider.class,
new WagonProvider()
{
@Override
public void release(Wagon wagon)
{
}

@Override
public Wagon lookup(String roleHint) throws Exception
{
if (roleHint != null && roleHint.startsWith("http"))
{
return new HttpWagon();
}
return null;
}
});
locator.addService(RepositoryConnectorFactory.class, WagonRepositoryConnectorFactory.class);

final RepositorySystem repositorySystem = locator.getService(RepositorySystem.class);
return repositorySystem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
import org.jboss.forge.dependencies.builder.DependencyBuilder;
import org.jboss.forge.resource.FileResource;
import org.jboss.forge.resource.ResourceFactory;
import org.jboss.shrinkwrap.resolver.impl.maven.logging.LogTransferListener;
import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.collection.CollectRequest;
import org.sonatype.aether.collection.DependencyCollectionContext;
import org.sonatype.aether.collection.DependencySelector;
import org.sonatype.aether.collection.DependencyTraverser;
import org.sonatype.aether.graph.DependencyFilter;
import org.sonatype.aether.graph.DependencyNode;
import org.sonatype.aether.repository.LocalRepository;
Expand Down Expand Up @@ -136,7 +140,7 @@ public List<Coordinate> resolveVersions(DependencyQuery query)

/**
* Returns the versions of a specific artifact
*
*
* @param query
* @return
*/
Expand Down Expand Up @@ -232,19 +236,61 @@ public org.jboss.forge.dependencies.DependencyNode resolveDependencyHierarchy(De
RepositorySystem system = container.getRepositorySystem();
Settings settings = container.getSettings();
MavenRepositorySystemSession session = setupRepoSession(system, settings);
final CoordinateBuilder coord = CoordinateBuilder.create(query.getCoordinate());
Artifact queryArtifact = coordinateToMavenArtifact(coord);
CollectRequest collectRequest = new CollectRequest(new org.sonatype.aether.graph.Dependency(queryArtifact,
null), container.getEnabledRepositoriesFromProfile(settings));
session.setTransferListener(new LogTransferListener());

DependencyRequest dr = new DependencyRequest(collectRequest, new DependencyFilter()
session.setDependencyTraverser(new DependencyTraverser()
{
@Override
public boolean accept(DependencyNode node, List<DependencyNode> parents)
public boolean traverseDependency(org.sonatype.aether.graph.Dependency dependency)
{
if ("test".equals(dependency.getScope()))
{
return false;
}
if ("provided".equals(dependency.getScope()))
{
String classifier = dependency.getArtifact().getClassifier();
return "forge-addon".equals(classifier);
}
return true;
}

@Override
public DependencyTraverser deriveChildTraverser(DependencyCollectionContext context)
{
return this;
}
});
session.setDependencySelector(new DependencySelector()
{
@Override
public boolean selectDependency(org.sonatype.aether.graph.Dependency dependency)
{
if ("test".equals(dependency.getScope()))
{
return false;
}
if ("provided".equals(dependency.getScope()))
{
String classifier = dependency.getArtifact().getClassifier();
return "forge-addon".equals(classifier);
}
return true;
}

@Override
public DependencySelector deriveChildSelector(DependencyCollectionContext context)
{
return this;
}
});

final CoordinateBuilder coord = CoordinateBuilder.create(query.getCoordinate());
Artifact queryArtifact = coordinateToMavenArtifact(coord);
CollectRequest collectRequest = new CollectRequest(new org.sonatype.aether.graph.Dependency(queryArtifact,
null), container.getEnabledRepositoriesFromProfile(settings));

DependencyRequest dr = new DependencyRequest(collectRequest, null);

DependencyResult result = system.resolveDependencies(session, dr);
return MavenConvertUtils.toDependencyNode(factory, null, result.getRoot());
Expand All @@ -254,5 +300,4 @@ public boolean accept(DependencyNode node, List<DependencyNode> parents)
throw new DependencyException("Could not resolve dependencies for addon [" + query.getCoordinate() + "]", e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

/**
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*
*/
public class MavenDependencyResolverTest
{
Expand Down Expand Up @@ -110,7 +110,16 @@ public void testResolveDependencyHierarchy() throws Exception
Assert.assertEquals(2, root.getChildren().size());
Assert.assertEquals("example2", root.getChildren().get(0).getDependency().getCoordinate().getArtifactId());
Assert.assertEquals("commons-lang", root.getChildren().get(1).getDependency().getCoordinate().getArtifactId());
System.out.println(root);
}

@Test
public void testResolveProvidedAddons() throws Exception
{
DependencyNode root = resolver
.resolveDependencyHierarchy(DependencyQueryBuilder
.create("org.jboss.forge:addons:jar:forge-addon:2.0.0-SNAPSHOT"));
// System.out.println(root);
Assert.assertNotNull(root);
Assert.assertEquals(2, root.getChildren().size());
}
}

0 comments on commit 8056c3a

Please sign in to comment.