Skip to content

Commit

Permalink
Added support for module jdks. Set default for export to true for COM…
Browse files Browse the repository at this point in the history
…PILE and RUNTIME scope. Fixed an issue with dependencies that are equal but that have different artifacts.
  • Loading branch information
Hans Dockter authored and Hans Dockter committed Jun 3, 2010
1 parent 55a1a66 commit e01a462
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 26 deletions.
42 changes: 29 additions & 13 deletions src/main/groovy/org/gradle/plugins/idea/IdeaModule.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,20 @@ public class IdeaModule extends DefaultTask {
/**
* If this variable is set, dependencies in the existing iml file will be parsed for this variable.
* If they use it, it will be replaced with a path that has the $MODULE_DIR$ variable as a root and
* then a relative path to {@link #gradleCacheHome}. That way Gradle can recognize equal dependencies.
* then a relative path to {@link #gradleCacheHome} . That way Gradle can recognize equal dependencies.
*/
@Input @Optional
String gradleCacheVariable

@InputFiles @Optional
File gradleCacheHome

@Input @Optional
String javaVersion = Module.INHERITED

/**
* The keys of this map are the Intellij scopes. Each key points to another map that has two keys, plus and minus.
* The values of those keys are sets of {@link org.gradle.api.artifacts.Configuration} objects. The files of the
* The values of those keys are sets of {@link org.gradle.api.artifacts.Configuration} objects. The files of the
* plus configurations are added minus the files from the minus configurations.
*/
Map scopes = [:]
Expand All @@ -116,7 +119,7 @@ public class IdeaModule extends DefaultTask {
void updateXML() {
Reader xmlreader = outputFile.exists() ? new FileReader(outputFile) : null;
Module module = new Module(getSourcePaths(), getTestSourcePaths(), getExcludePaths(), getOutputPath(), getTestOutputPath(),
getDependencies(), getVariableReplacement(), xmlreader, beforeConfiguredActions, whenConfiguredActions, withXmlActions)
getDependencies(), getVariableReplacement(), javaVersion, xmlreader, beforeConfiguredActions, whenConfiguredActions, withXmlActions)
module.toXml(new FileWriter(outputFile))
}

Expand Down Expand Up @@ -156,7 +159,7 @@ public class IdeaModule extends DefaultTask {

protected Set getModules(String scope) {
if (scopes[scope]) {
return getScopeDependencies(scopes[scope], ProjectDependency).collect { ProjectDependency projectDependency ->
return getScopeDependencies(scopes[scope], { it instanceof ProjectDependency}).collect { ProjectDependency projectDependency ->
projectDependency.dependencyProject
}.collect { project ->
new ModuleDependency(project.name, scope)
Expand All @@ -167,18 +170,18 @@ public class IdeaModule extends DefaultTask {

protected Set getModuleLibraries(String scope) {
if (scopes[scope]) {
Set firstLevelDependencies = getScopeDependencies(scopes[scope], ExternalDependency)
Set firstLevelDependencies = getScopeDependencies(scopes[scope], { it instanceof ExternalDependency})

ResolvedConfiguration resolvedConfiguration = project.configurations.detachedConfiguration((firstLevelDependencies as Dependency[])).resolvedConfiguration
def allResolvedDependencies = getAllDeps(resolvedConfiguration.firstLevelModuleDependencies)

Set sourceDependencies = getResolvableDependenciesForAllResolvedDependencies(allResolvedDependencies) { dependency ->
addSourceArtifact(dependency)
}
Map sourceFiles = downloadSources ? getFiles(sourceDependencies, "sources") : [:]
Map sourceFiles = downloadSources ? getFiles(sourceDependencies, "sources") : [:]

Set javadocDependencies = getResolvableDependenciesForAllResolvedDependencies(allResolvedDependencies) { dependency ->
addJavadocArtifact(dependency)
addJavadocArtifact(dependency)
}
Map javadocFiles = downloadJavadoc ? getFiles(javadocDependencies, "javadoc") : [:]

Expand All @@ -187,7 +190,8 @@ public class IdeaModule extends DefaultTask {
File javadocFile = javadocFiles[binaryFile.name]
new ModuleLibrary([getPath(binaryFile)] as Set, javadocFile ? [getPath(javadocFile)] as Set : [] as Set, sourceFile ? [getPath(sourceFile)] as Set : [] as Set, [] as Set, scope)
}
moduleLibraries.addAll(getSelfResolvingFiles(getScopeDependencies(scopes[scope], SelfResolvingDependency), scope))
moduleLibraries.addAll(getSelfResolvingFiles(getScopeDependencies(scopes[scope],
{ it instanceof SelfResolvingDependency && !(it instanceof ProjectDependency)}), scope))
return moduleLibraries
}
return []
Expand All @@ -202,13 +206,25 @@ public class IdeaModule extends DefaultTask {
}
}

private Set getScopeDependencies(Map configurations, Class type) {
private Set getScopeDependencies(Map configurations, Closure filter) {
Set firstLevelDependencies = []
configurations.plus.each { configuration ->
firstLevelDependencies += configuration.getAllDependencies(type)
firstLevelDependencies += configuration.getAllDependencies().findAll(filter)
}
configurations.minus.each { configuration ->
firstLevelDependencies -= configuration.getAllDependencies(type)
configuration.getAllDependencies().findAll(filter).each { minusDep ->
// This deals with dependencies that are defined in different scopes with different
// artifacts. Right now we accept the fact, that in such a situation some artifacts
// might be duplicated in Idea (they live in different scopes then).
if (minusDep instanceof ExternalDependency) {
ExternalDependency removeCandidate = firstLevelDependencies.find { it == minusDep }
if (removeCandidate.artifacts == minusDep.artifacts) {
firstLevelDependencies.remove(removeCandidate)
}
} else {
firstLevelDependencies.remove(minusDep)
}
}
}
return firstLevelDependencies
}
Expand All @@ -234,7 +250,7 @@ public class IdeaModule extends DefaultTask {
protected Set getAllDeps(Set deps) {
Set result = []
deps.each { ResolvedDependency resolvedDependency ->
if(resolvedDependency.children) {
if (resolvedDependency.children) {
result.addAll(getAllDeps(resolvedDependency.children))
}
result.add(resolvedDependency)
Expand Down
45 changes: 40 additions & 5 deletions src/main/groovy/org/gradle/plugins/idea/model/Module.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.gradle.listener.ListenerBroadcast
* @author Hans Dockter
*/
class Module {
static final String INHERITED = "inherited"
/**
* The foldes for the production code. Must not be null.
*/
Expand All @@ -50,17 +51,20 @@ class Module {
Path testOutputDir

/**
* The dependencies of this module. Must not be null. Has instances of type {@link Dependency}.
* The dependencies of this module. Must not be null. Has instances of type {@link Dependency} .
*/
Set dependencies = [] as LinkedHashSet


String javaVersion

private Node xml

private ListenerBroadcast<Action> withXmlActions

def Module(Set sourceFolders, Set testSourceFolders, Set excludeFolders, Path outputDir, Path testOutputDir, Set dependencies,
VariableReplacement dependencyVariableReplacement, Reader inputXml, ListenerBroadcast<Action> beforeConfiguredActions,
ListenerBroadcast<Action> whenConfiguredActions, ListenerBroadcast<Action> withXmlActions) {
VariableReplacement dependencyVariableReplacement, String javaVersion, Reader inputXml,
ListenerBroadcast<Action> beforeConfiguredActions, ListenerBroadcast<Action> whenConfiguredActions,
ListenerBroadcast<Action> withXmlActions) {
initFromXml(inputXml, dependencyVariableReplacement)

beforeConfiguredActions.source.execute(this)
Expand All @@ -71,6 +75,7 @@ class Module {
if (outputDir) this.outputDir = outputDir;
if (testOutputDir) this.testOutputDir = testOutputDir;
this.dependencies.addAll(dependencies);
if (javaVersion) this.javaVersion = javaVersion
this.withXmlActions = withXmlActions;

whenConfiguredActions.source.execute(this)
Expand All @@ -79,12 +84,21 @@ class Module {
private def initFromXml(Reader inputXml, VariableReplacement dependencyVariableReplacement) {
Reader reader = inputXml ?: new InputStreamReader(getClass().getResourceAsStream('defaultModule.xml'))
xml = new XmlParser().parse(reader)

readJdkFromXml()
readSourceAndExcludeFolderFromXml()
readOutputDirsFromXml()
readDependenciesFromXml(dependencyVariableReplacement)
}

private def readJdkFromXml() {
def jdk = findOrderEntries().find { it.@type == 'jdk' }
if (jdk) {
this.javaVersion = jdk.@jdkName
} else {
this.javaVersion = INHERITED
}
}

private def readOutputDirsFromXml() {
def outputDirUrl = findOutputDir()?.@url
def testOutputDirUrl = findTestOutputDir()?.@url
Expand Down Expand Up @@ -134,6 +148,7 @@ class Module {
* @param writer The writer where the iml xml is generated into.
*/
def toXml(Writer writer) {
addJdkToXml()
removeSourceAndExcludeFolderFromXml()
addSourceAndExcludeFolderToXml()
addOutputDirsToXml()
Expand All @@ -146,6 +161,26 @@ class Module {
new XmlNodePrinter(new PrintWriter(writer)).print(xml)
}

private def addJdkToXml() {
assert javaVersion != null
Node moduleJdk = findOrderEntries().find { it.@type == 'jdk' }
if (javaVersion != INHERITED) {
Node inheritedJdk = findOrderEntries().find { it.@type == "inheritedJdk" }
if (inheritedJdk) {
inheritedJdk.parent().remove(inheritedJdk)
}
if (moduleJdk) {
findNewModuleRootManager().remove(moduleJdk)
}
findNewModuleRootManager().appendNode("orderEntry", [type: "jdk", jdkName: javaVersion, jdkType: "JavaSDK"])
} else if (!(findOrderEntries().find { it.@type == "inheritedJdk" })) {
if (moduleJdk) {
findNewModuleRootManager().remove(moduleJdk)
}
findNewModuleRootManager().appendNode("orderEntry", [type: "inheritedJdk"])
}
}

private def addOutputDirsToXml() {
if (outputDir) {
findOrCreateOutputDir().@url = outputDir.url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ class ModuleDependency implements Dependency {
*/
String scope

boolean exported = false
boolean exported

def ModuleDependency(name, scope) {
this.name = name;
this.scope = scope;
this.exported = !scope || scope == 'COMPILE' || scope == 'RUNTIME'
}

void addToNode(Node parentNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ class ModuleLibrary implements Dependency {
*/
String scope

boolean exported = false
boolean exported

def ModuleLibrary(classes, javadoc, sources, jarDirectories, scope) {
this.classes = classes;
this.jarDirectories = jarDirectories;
this.javadoc = javadoc;
this.sources = sources;
this.scope = scope
this.exported = !scope || scope == 'COMPILE' || scope == 'RUNTIME'
}

void addToNode(Node parentNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,13 @@ class ModuleDependencyTest extends Specification {
new ModuleDependency("a", null).hashCode() == new ModuleDependency("a", "COMPILE").hashCode()
new ModuleDependency("a", "").hashCode() == new ModuleDependency("a", "COMPILE").hashCode()
}

def shouldExportForCompileAndRuntimeScope() {
expect:
new ModuleDependency("a", "COMPILE").exported
new ModuleDependency("a", "RUNTIME").exported
new ModuleDependency("a", "").exported
new ModuleDependency("a", null).exported
!(new ModuleDependency("a", "TEST").exported)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ class ModuleLibraryTest extends Specification {
private ModuleLibrary createModuleLibraryWithScope(String scope) {
new ModuleLibrary([] as Set, [] as Set, [] as Set, [] as Set, scope)
}

def shouldExportForCompileAndRuntimeScope() {
expect:
createModuleLibraryWithScope("COMPILE").exported
createModuleLibraryWithScope("RUNTIME").exported
createModuleLibraryWithScope("").exported
createModuleLibraryWithScope(null).exported
!(createModuleLibraryWithScope("TEST").exported)
}
}
14 changes: 9 additions & 5 deletions src/test/groovy/org/gradle/plugins/idea/model/ModuleTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ModuleTest extends Specification {
module = createModule(reader: customModuleReader)

expect:
module.javaVersion == "1.6"
module.sourceFolders == CUSTOM_SOURCE_FOLDERS
module.testSourceFolders == CUSTOM_TEST_SOURCE_FOLDERS
module.excludeFolders == CUSTOM_EXCLUDE_FOLDERS
Expand Down Expand Up @@ -80,20 +81,22 @@ class ModuleTest extends Specification {
def constructorTestSourceFolders = [new Path('b')] as Set
def constructorExcludeFolders = [new Path('c')] as Set
def constructorOutputDir = new Path('someOut')
def constructorJavaVersion = '1.6'
def constructorTestOutputDir = new Path('someTestOut')
def constructorModuleDependencies = [
CUSTOM_DEPENDENCIES[0],
new ModuleLibrary([new Path('x')], [], [], [new JarDirectory(new Path('y'), false)], null)] as LinkedHashSet
module = createModule(sourceFolders: constructorSourceFolders, testSourceFolders: constructorTestSourceFolders,
excludeFolders: constructorExcludeFolders, outputDir: constructorOutputDir, testOutputDir: constructorTestOutputDir,
moduleLibraries: constructorModuleDependencies, reader: customModuleReader)
moduleLibraries: constructorModuleDependencies, javaVersion: constructorJavaVersion, reader: customModuleReader)

expect:
module.sourceFolders == CUSTOM_SOURCE_FOLDERS + constructorSourceFolders
module.testSourceFolders == CUSTOM_TEST_SOURCE_FOLDERS + constructorTestSourceFolders
module.excludeFolders == CUSTOM_EXCLUDE_FOLDERS + constructorExcludeFolders
module.outputDir == constructorOutputDir
module.testOutputDir == constructorTestOutputDir
module.javaVersion == constructorJavaVersion
module.dependencies == (CUSTOM_DEPENDENCIES as LinkedHashSet) + constructorModuleDependencies
}

Expand All @@ -102,6 +105,7 @@ class ModuleTest extends Specification {
module = createModule(sourceFolders: constructorSourceFolders)

expect:
module.javaVersion == Module.INHERITED
module.sourceFolders == constructorSourceFolders
module.dependencies.size() == 0
}
Expand All @@ -120,7 +124,7 @@ class ModuleTest extends Specification {
def constructorTestOutputDir = new Path('someTestOut')

when:
this.module = createModule(sourceFolders: constructorSourceFolders, reader: defaultModuleReader,
this.module = createModule(javaVersion: '1.6', sourceFolders: constructorSourceFolders, reader: defaultModuleReader,
outputDir: constructorOutputDir, testOutputDir: constructorTestOutputDir)
def module = createModule(reader: toXmlReader)

Expand Down Expand Up @@ -201,10 +205,10 @@ class ModuleTest extends Specification {
private Module createModule(Map customArgs) {
ListenerBroadcast dummyBroadcast = new ListenerBroadcast(Action)
Map args = [sourceFolders: [] as Set, testSourceFolders: [] as Set, excludeFolders: [] as Set, outputDir: null, testOutputDir: null,
moduleLibraries: [] as Set, dependencyVariableReplacement: VariableReplacement.NO_REPLACEMENT, reader: null,
beforeConfiguredActions: dummyBroadcast, whenConfiguredActions: dummyBroadcast, withXmlActions: dummyBroadcast] + customArgs
moduleLibraries: [] as Set, dependencyVariableReplacement: VariableReplacement.NO_REPLACEMENT, javaVersion: null,
reader: null, beforeConfiguredActions: dummyBroadcast, whenConfiguredActions: dummyBroadcast, withXmlActions: dummyBroadcast] + customArgs
return new Module(args.sourceFolders, args.testSourceFolders, args.excludeFolders, args.outputDir, args.testOutputDir,
args.moduleLibraries, args.dependencyVariableReplacement, args.reader,
args.moduleLibraries, args.dependencyVariableReplacement, args.javaVersion, args.reader,
args.beforeConfiguredActions, args.whenConfiguredActions, args.withXmlActions)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<output url="file://$MODULE_DIR$/out"/>
<output-test url="file://$MODULE_DIR$/outTest"/>
<exclude-output/>
<orderEntry type="inheritedJdk"/>
<orderEntry type="jdk" jdkName="1.6" jdkType="JavaSDK" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false"/>
<sourceFolder url="file://$MODULE_DIR$/srcTest" isTestSource="true"/>
Expand Down
18 changes: 18 additions & 0 deletions testProject/services/webservice/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,26 @@ apply plugin: 'war'

version = '2.5'

ideaModule {
javaVersion = '1.5'
}

dependencies {
compile project(':shared'), 'commons-collections:commons-collections:3.2@jar', 'commons-io:commons-io:1.2', 'commons-lang:commons-lang:2.4@jar'
compile project(path: ':api', configuration: 'spi')
runtime project(':api')
compile group: 'org.infinispan', name: 'infinispan-core', version: '4.0.0.FINAL'
testCompile (
[group: 'org.infinispan', name: 'infinispan-core', version: '4.0.0.FINAL']
) {
artifact {
name = "infinispan-core"
type = 'jar'
}
artifact {
name = "infinispan-core"
classifier = 'tests'
type = 'jar'
}
}
}

0 comments on commit e01a462

Please sign in to comment.