Skip to content
Browse files

Support for parsing dependencies from pom.xml so that Maven users can…

… use regular Grails commands
  • Loading branch information...
1 parent e5e81aa commit 082fd182ed7a18065369afff70a204756ec97c87 @graemerocher graemerocher committed
View
60 grails/src/java/org/codehaus/groovy/grails/resolve/IvyDependencyManager.groovy
@@ -39,6 +39,12 @@ import org.codehaus.groovy.grails.resolve.EnhancedDefaultDependencyDescriptor
import grails.util.BuildSettings
import org.apache.ivy.core.module.descriptor.ExcludeRule
import grails.util.GrailsNameUtils
+import org.apache.ivy.plugins.parser.m2.PomReader
+import org.apache.ivy.plugins.repository.file.FileResource
+import org.apache.ivy.plugins.repository.file.FileRepository
+import org.apache.ivy.plugins.parser.m2.PomDependencyMgt
+import org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor
+import org.apache.ivy.core.module.id.ModuleId
/**
* Implementation that uses Apache Ivy under the hood
@@ -90,6 +96,7 @@ public class IvyDependencyManager implements DependencyResolver, DependencyDefin
static List<Configuration> ALL_CONFIGURATIONS = [BUILD_CONFIGURATION, COMPILE_CONFIGURATION, RUNTIME_CONFIGURATION, TEST_CONFIGURATION, PROVIDED_CONFIGURATION]
+ private Set<ModuleId> modules = [] as Set
private Set<ModuleRevisionId> dependencies = [] as Set
private Set<DependencyDescriptor> dependencyDescriptors = [] as Set
private orgToDepMap = [:]
@@ -112,6 +119,8 @@ public class IvyDependencyManager implements DependencyResolver, DependencyDefin
List repositoryData = []
Set<String> configuredPlugins = [] as Set
Set<String> usedConfigurations = [] as Set
+ boolean readPom = false
+
private static managers = new ConcurrentHashMap()
private static currentManager
@@ -354,6 +363,7 @@ public class IvyDependencyManager implements DependencyResolver, DependencyDefin
* Adds a dependency to the project
*/
void addDependency(ModuleRevisionId revisionId) {
+ modules << revisionId.moduleId
dependencies << revisionId
if(orgToDepMap[revisionId.organisation]) {
orgToDepMap[revisionId.organisation] << revisionId
@@ -469,6 +479,22 @@ public class IvyDependencyManager implements DependencyResolver, DependencyDefin
}
}
+
+
+ /**
+ * Tests whether the given ModuleId is defined in the list of dependencies
+ */
+ boolean hasDependency(ModuleId mid) {
+ return modules.contains(mid)
+ }
+
+ /**
+ * Tests whether the given group and name are defined in the list of dependencies
+ */
+ boolean hasDependency(String group, String name) {
+ return hasDependency(ModuleId.newInstance(group, name))
+ }
+
/**
* Parses the Ivy DSL definition
*/
@@ -491,9 +517,39 @@ public class IvyDependencyManager implements DependencyResolver, DependencyDefin
definition.resolveStrategy = Closure.DELEGATE_FIRST
definition()
+ if(readPom && buildSettings) {
+ List dependencies = readDependenciesFromPOM()
+
+ if(dependencies!=null){
+ for(PomDependencyMgt dep in dependencies) {
+ final String scope = dep.scope ?: 'runtime'
+ Message.debug("Read dependency [$dep.groupId:$dep.artifactId:$dep.version] (scope $scope) from Maven pom.xml")
+
+ def mrid = ModuleRevisionId.newInstance(dep.groupId, dep.artifactId, dep.version)
+ def mid = mrid.getModuleId()
+ if(!hasDependency(mid)) {
+ def dd = new EnhancedDefaultDependencyDescriptor(mrid, false, true, scope)
+ configureDependencyDescriptor(dd, scope)
+ addDependencyDescriptor dd
+ }
+ }
+ }
+ }
+
}
}
+ List readDependenciesFromPOM() {
+ def dependencies = null
+ def pom = new File("${buildSettings.baseDir.path}/pom.xml")
+ if (pom.exists()) {
+ def reader = new PomReader(pom.toURL(), new FileResource(new FileRepository(), pom))
+
+ dependencies = reader.getDependencies()
+ }
+ return dependencies
+ }
+
/**
* Parses dependencies of a plugin
@@ -529,6 +585,10 @@ class IvyDomainSpecificLanguageEvaluator {
this.delegate = delegate
}
+ void pom(boolean b) {
+ delegate.readPom = b
+ }
+
void excludes(String... excludes) {
for(name in excludes ) {
moduleExcludes << name
View
31 grails/src/test/org/codehaus/groovy/grails/resolve/IvyDependencyManagerTests.groovy
@@ -9,6 +9,7 @@ import grails.util.BuildSettings
import grails.util.BuildSettingsHolder
import grails.util.GrailsUtil
import groovy.xml.MarkupBuilder
+import org.apache.ivy.plugins.parser.m2.PomDependencyMgt
/**
* @author Graeme Rocher
@@ -25,6 +26,23 @@ public class IvyDependencyManagerTests extends GroovyTestCase{
GroovySystem.metaClassRegistry.removeMetaClass(System)
}
+ void testReadMavenPom() {
+ def settings = new BuildSettings()
+ def manager = new DummyMavenAwareDependencyManager("test", "0.1",settings)
+
+ assertFalse "shouldn't be reading POM", manager.readPom
+
+ manager.parseDependencies {
+ pom true
+ }
+
+ assertTrue "should be reading POM", manager.readPom
+ def deps = manager.listDependencies("test")
+ assertEquals 1, deps.size()
+ assertEquals "junit", deps[0].moduleId.name
+
+ }
+
void testHasApplicationDependencies() {
def settings = new BuildSettings()
def manager = new IvyDependencyManager("test", "0.1",settings)
@@ -322,4 +340,17 @@ public class IvyDependencyManagerTests extends GroovyTestCase{
}
}
+}
+class DummyMavenAwareDependencyManager extends IvyDependencyManager {
+
+ public DummyMavenAwareDependencyManager(String applicationName, String applicationVersion, BuildSettings settings) {
+ super(applicationName, applicationVersion, settings);
+ }
+
+ public List readDependenciesFromPOM() {
+ return [
+ [getGroupId:{"junit"}, getArtifactId:{"junit"}, getVersion:{"3.8.3"}, getScope:{"test"}] as PomDependencyMgt
+ ]
+ }
+
}

0 comments on commit 082fd18

Please sign in to comment.
Something went wrong with that request. Please try again.