-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathstatic-weaving.gradle
More file actions
189 lines (162 loc) · 7.88 KB
/
Copy pathstatic-weaving.gradle
File metadata and controls
189 lines (162 loc) · 7.88 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
private static List<String> findJpaManagedClasses(Project moduleProject) {
File sourceRoot = moduleProject.file('src/main/java')
if (!sourceRoot.exists()) {
return []
}
moduleProject.fileTree(sourceRoot) {
include '**/*.java'
}.findAll { File javaSource ->
isJpaManagedClass(javaSource.text)
}.collect { File javaSource ->
toFullyQualifiedClassName(javaSource)
}.findAll {
it != null
}.unique().sort()
}
private static boolean isJpaManagedClass(String source) {
def hasManagedAnnotation = (source =~ /(?m)^\s*@((jakarta|javax)\.persistence\.)?(Entity|MappedSuperclass|Converter)\b/).find()
def isConcreteAttributeConverter = (source =~ /(?s)\bimplements\b.*?\bAttributeConverter\s*</).find()
&& !(source =~ /(?m)^\s*(public\s+|protected\s+|private\s+)?abstract\s+class\s+/).find()
hasManagedAnnotation || isConcreteAttributeConverter
}
private static String toFullyQualifiedClassName(File javaSource) {
def packageMatcher = javaSource.text =~ /(?m)^\s*package\s+([^;]+);/
if (!packageMatcher.find()) {
return null
}
"${packageMatcher.group(1)}.${javaSource.name - '.java'}"
}
private static List<Project> runtimeProjectDependencies(Project moduleProject) {
def runtimeClasspath = moduleProject.configurations.findByName('runtimeClasspath')
if (runtimeClasspath == null || !runtimeClasspath.canBeResolved) {
return []
}
runtimeClasspath.incoming.resolutionResult.allComponents.collect { component ->
component.id
}.findAll { componentId ->
componentId instanceof ProjectComponentIdentifier && componentId.projectPath != moduleProject.path
}.collect { componentId ->
moduleProject.rootProject.project(componentId.projectPath)
}.unique().sort { left, right ->
left.path <=> right.path
}
}
private static Map<Project, List<String>> managedClassesByProject(Project moduleProject) {
(runtimeProjectDependencies(moduleProject) + moduleProject).collectEntries { Project candidateProject ->
def managedClasses = findJpaManagedClasses(candidateProject)
managedClasses.isEmpty() ? [:] : [(candidateProject): managedClasses]
}
}
private static void writePersistenceXml(File outputFile, Map<Project, List<String>> classesByProject) {
outputFile.parentFile.mkdirs()
outputFile.text = """<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<!-- This file is generated by static-weaving.gradle and is only used for static weaving. -->
<!-- You can find the runtime configuration in the JPAConfig class. -->
<persistence-unit name="jpa-pu" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
${classesByProject.collect { Project moduleProject, List<String> managedClasses ->
"""
<!-- ${moduleProject.name} module: -->
${managedClasses.collect { managedClass ->
" <class>${managedClass}</class>"
}.join('\n')}"""
}.join('\n')}
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.weaving" value="static"/>
<property name="eclipselink.weaving.internal" value="false"/>
</properties>
</persistence-unit>
</persistence>
"""
}
// Wait until after project evaluation to ensure all plugins and dependencies are applied.
project.afterEvaluate {
if (!project.plugins.hasPlugin('java')) {
logger.info("Skipping static weaving configuration for non-Java project: ${project.name}")
return
}
def ownManagedClasses = findJpaManagedClasses(project)
if (ownManagedClasses.isEmpty()) {
logger.info("No JPA managed classes found in ${project.name}, skipping static weaving configuration")
return
}
logger.info("Configuring EclipseLink static weaving for ${project.name}")
def generatedPersistenceXml = layout.buildDirectory.file('tmp/compileJava/static-weaving/META-INF/persistence.xml')
def generateStaticWeavingPersistenceXml = tasks.register('generateStaticWeavingPersistenceXml') {
description = 'Generates the EclipseLink static weaving persistence.xml from project JPA classes'
group = 'build'
inputs.files(project.sourceSets.main.java.srcDirs).withPropertyName('javaSourceRoots')
outputs.file(generatedPersistenceXml).withPropertyName('persistenceXml')
outputs.upToDateWhen { false }
doLast {
def classesByProject = managedClassesByProject(project)
def classCount = classesByProject.values().sum { List<String> managedClasses -> managedClasses.size() } ?: 0
writePersistenceXml(generatedPersistenceXml.get().asFile, classesByProject)
logger.lifecycle("Generated EclipseLink static weaving persistence.xml for ${project.path} with ${classCount} managed classes from ${classesByProject.size()} modules")
}
}
tasks.named('compileJava') {
dependsOn(generateStaticWeavingPersistenceXml)
doLast {
File persistenceXml = generatedPersistenceXml.get().asFile
if (!persistenceXml.exists()) {
logger.info("No generated persistence.xml found for ${project.name}, skipping static weaving")
return
}
File source = destinationDirectory.get().asFile
File weavingRoot = persistenceXml.parentFile.parentFile
project.javaexec {
description = 'Performs EclipseLink static weaving of entity classes'
mainClass.set('org.eclipse.persistence.tools.weaving.jpa.StaticWeave')
classpath = project.sourceSets.main.runtimeClasspath
args = [
'-persistenceinfo',
weavingRoot.absolutePath,
source.absolutePath,
source.absolutePath
]
}
}
}
logger.info("EclipseLink static weaving configured for ${project.name}")
}