-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean-pom.gradle
143 lines (127 loc) · 6.08 KB
/
clean-pom.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
apply plugin: 'maven'
/*
* this task writes out a pom.xml file which can be imported as project into eclipse.
* we don't like the gradle integration in eclipse, it's slow for us. maven is faster.
*/
task pom {
doLast {
pom {
/*
* SET SOURCE ENCODING TO UTF-8
*/
project {
properties { 'project.build.sourceEncoding' 'UTF-8' }
// PARENT PROJECT
if (subprojects && subprojects.size() > 0) {
'modules' {
subprojects.each {
module it.name
}
}
packaging 'pom'
}
// SUBMODULE
if (project.parent) {
parent {
artifactId project.parent.name
groupId project.group
version project.version
}
}
}
}.withXml { xmlProvider ->
/*
* SORT dependencyies by groupId and artifactId, otherwise their order will change on each invocation
*/
def node = xmlProvider.asNode()
/*
* gradle pom task writes groupId and version for parent project which irritatates eclipse sometimes.
* remove it
*/
if (node.get('parent')) {
node.remove(node.get('groupId'))
node.remove(node.get('version'))
}
/*
* We need to add the compileOnly dependencies manually, maven-plugin isn't supporting it.
*/
project.configurations.compileOnly.allDependencies.each { dep ->
asNode().dependencies[0].appendNode('dependency').with {
it.appendNode('groupId', dep.group)
it.appendNode('artifactId', dep.name)
it.appendNode('version', dep.version)
it.appendNode('scope', 'provided')
}
}
def dependenciesNodeList = node.get('dependencies')
if (dependenciesNodeList) {
def dependenciesNode = dependenciesNodeList.get(0)
/*
* Newer Eclipse versions need test-jars if you want to share test code.
* Just adding "type: test-jar" isn't enough, project won't run anymore,
* we have to duplicate the dependency instead.
*/
if (project.hasProperty('addTestJars')) {
for (def i = 0; i < dependenciesNode.children().size(); i++) {
def child = dependenciesNode.children().get(i)
def groupId = child.get('groupId').text().trim()
def artifactId = child.get('artifactId').text().trim()
def fullId = groupId + '.' + artifactId
if (project.addTestJars.contains(groupId) || project.addTestJars.contains(fullId)) {
if (child.get('scope').text().trim().equals('tests') == false) {
dependenciesNode.appendNode('dependency').with {
it.appendNode('groupId', child.get('groupId').text().trim())
it.appendNode('artifactId', child.get('artifactId').text().trim())
it.appendNode('version', child.get('version').text().trim())
it.appendNode('scope', 'tests')
it.appendNode('type', 'test-jar')
}
}
}
}
}
def dependencyNodeList = dependenciesNode.get('dependency')
def sorted = dependencyNodeList.sort { a, b ->
def r = a.get('groupId').text().trim().compareTo(b.get('groupId').text().trim())
if (r != 0) return r
return a.get('artifactId').text().trim().compareTo(b.get('artifactId').text().trim())
}
dependenciesNode.children().removeAll(dependenciesNode.children())
sorted.each { dependenciesNode.append(it) }
}
/*
* add maven-compiler-plugin for eclipse
*/
def plugin = node.appendNode('build').appendNode('plugins').appendNode('plugin')
plugin.appendNode('groupId').value = 'org.apache.maven.plugins'
plugin.appendNode('artifactId').value = 'maven-compiler-plugin'
plugin.appendNode('version').value = '3.8.1'
def configuration = plugin.appendNode('configuration')
if (project.sourceCompatibility.isJava10Compatible()) {
configuration.appendNode('source').value = project.sourceCompatibility.getMajorVersion()
}
else {
configuration.appendNode('source').value = project.sourceCompatibility
}
if (project.targetCompatibility.isJava10Compatible()) {
configuration.appendNode('target').value = project.targetCompatibility.getMajorVersion()
}
else {
configuration.appendNode('target').value = project.targetCompatibility
}
/**
* Add annotation processors
*/
if (project.configurations.annotationProcessor.allDependencies.isEmpty() == false) {
def annotationProcessorPaths = configuration.appendNode('annotationProcessorPaths')
project.configurations.annotationProcessor.allDependencies.each { dep ->
annotationProcessorPaths.appendNode('path').with {
it.appendNode('groupId', dep.group)
it.appendNode('artifactId', dep.name)
it.appendNode('version', dep.version)
}
}
}
}.writeTo('pom.xml')
}
}