Last active
December 16, 2015 07:48
-
-
Save ritalin/5401261 to your computer and use it in GitHub Desktop.
Gradleのスクリプトから、Eclipse Maven Project吐くためのタスクをプラグイン化 based on https://gist.github.com/ritalin/5394398
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: SyncPomPlugin | |
class SyncPomPlugin implements Plugin<Project> { | |
void apply(Project project) { | |
project.task('to-pom', type: ToPomTask).dependsOn('eclipse') | |
} | |
} | |
class ToPomTask extends DefaultTask { | |
def ToPomTask() { | |
project.eclipse { | |
project { | |
natures = [ | |
'org.eclipse.jdt.core.javanature', | |
'org.eclipse.m2e.core.maven2Nature' | |
] | |
buildCommand 'org.eclipse.m2e.core.maven2Builder' | |
} | |
classpath { | |
containers = [ | |
'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7', | |
'org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER' | |
] | |
file { | |
whenMerged { classpath -> | |
classpath.configure classpath.entries.grep { entry -> | |
!(entry instanceof org.gradle.plugins.ide.eclipse.model.Library) | |
} | |
} | |
} | |
} | |
} | |
} | |
@TaskAction | |
def 'to-pom'() { | |
def dir = "${project.projectDir.absolutePath}".replaceAll('[\\\\]', '/') | |
def parser = new XmlSlurper(false, false) | |
def doc = parser.parse(new File("${dir}/pom.xml.default")) | |
def repos = doc.repositories.repository | |
.collect { repo -> | |
repo.url.toString() | |
} | |
.toSet() | |
def celtralRepo = project.repositories.mavenCentral().getUrl().toString() | |
def reposDiff = project.repositories.grep { | |
def url = it.getUrl().toString() | |
(url != celtralRepo) && (! repos.contains(url)) | |
} | |
reposDiff.each { r -> | |
def repoUrl = r.getUrl().toString() | |
def repoId = r.getUrl().getHost() | |
doc.repositories.appendNode { | |
repository { | |
'id' repoId | |
'name ' repoId | |
'url' repoUrl | |
} | |
} | |
} | |
def deps = doc.dependencies.dependency | |
.collect { | |
new Tuple(it.groupId.toString(), it.artifactId.toString(), it.version.toString()) | |
} | |
.toSet() | |
def depsDiff = project.configurations.compile.dependencies | |
.grep { | |
! deps.contains(new Tuple(it.getGroup(), it.getName(), it.getVersion())) | |
} | |
depsDiff.each { d -> | |
doc.dependencies.appendNode { | |
dependency { | |
'groupId' d.getGroup() | |
'artifactId' d.getName() | |
'version ' d.getVersion() | |
} | |
} | |
} | |
def output = new File("${dir}/pom.xml"); | |
def xml = groovy.xml.XmlUtil.serialize(new groovy.xml.StreamingMarkupBuilder().bind { | |
mkp.yield doc | |
}) | |
output.write(xml) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment