-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
ConsumerPomArtifactTransformer.java
197 lines (178 loc) · 7.9 KB
/
ConsumerPomArtifactTransformer.java
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
190
191
192
193
194
195
196
197
/*
* 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.
*/
package org.apache.maven.internal.transformation;
import javax.annotation.PreDestroy;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.function.BiConsumer;
import org.apache.maven.feature.Features;
import org.apache.maven.model.building.DefaultBuildPomXMLFilterFactory;
import org.apache.maven.model.building.TransformerContext;
import org.apache.maven.model.transform.RawToConsumerPomXMLFilterFactory;
import org.apache.maven.model.transform.pull.XmlUtils;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifact;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
import org.codehaus.plexus.util.xml.pull.MXParser;
import org.codehaus.plexus.util.xml.pull.XmlPullParser;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.deployment.DeployRequest;
import org.eclipse.aether.installation.InstallRequest;
/**
* Consumer POM transformer.
*
* @since TBD
*/
@Singleton
@Named("consumer-pom")
public final class ConsumerPomArtifactTransformer {
private static final String CONSUMER_POM_CLASSIFIER = "consumer";
private final Set<Path> toDelete = new CopyOnWriteArraySet<>();
public void injectTransformedArtifacts(MavenProject project, RepositorySystemSession session) throws IOException {
if (project.getFile() == null) {
// If there is no build POM there is no reason to inject artifacts for the consumer POM.
return;
}
if (isActive(session)) {
Path generatedFile;
String buildDirectory =
project.getBuild() != null ? project.getBuild().getDirectory() : null;
if (buildDirectory == null) {
generatedFile = Files.createTempFile(CONSUMER_POM_CLASSIFIER, "pom");
} else {
Path buildDir = Paths.get(buildDirectory);
Files.createDirectories(buildDir);
generatedFile = Files.createTempFile(buildDir, CONSUMER_POM_CLASSIFIER, "pom");
}
deferDeleteFile(generatedFile);
project.addAttachedArtifact(new ConsumerPomArtifact(project, generatedFile, session));
} else if (project.getModel().isRoot()) {
throw new IllegalStateException(
"The use of the root attribute on the model requires the buildconsumer feature to be active");
}
}
private void deferDeleteFile(Path generatedFile) {
toDelete.add(generatedFile.toAbsolutePath());
}
@PreDestroy
private void doDeleteFiles() {
for (Path file : toDelete) {
try {
Files.delete(file);
} catch (IOException e) {
// ignore, we did our best...
}
}
}
public InstallRequest remapInstallArtifacts(RepositorySystemSession session, InstallRequest request) {
if (isActive(session) && consumerPomPresent(request.getArtifacts())) {
request.setArtifacts(replacePom(request.getArtifacts()));
}
return request;
}
public DeployRequest remapDeployArtifacts(RepositorySystemSession session, DeployRequest request) {
if (isActive(session) && consumerPomPresent(request.getArtifacts())) {
request.setArtifacts(replacePom(request.getArtifacts()));
}
return request;
}
private boolean isActive(RepositorySystemSession session) {
return Features.buildConsumer(session.getUserProperties()).isActive();
}
private boolean consumerPomPresent(Collection<Artifact> artifacts) {
return artifacts.stream().anyMatch(a -> CONSUMER_POM_CLASSIFIER.equals(a.getClassifier()));
}
private Collection<Artifact> replacePom(Collection<Artifact> artifacts) {
ArrayList<Artifact> result = new ArrayList<>(artifacts.size());
for (Artifact artifact : artifacts) {
if (CONSUMER_POM_CLASSIFIER.equals(artifact.getClassifier())) {
// if under CONSUMER_POM_CLASSIFIER, move it to "" classifier
DefaultArtifact remapped = new DefaultArtifact(
artifact.getGroupId(),
artifact.getArtifactId(),
"",
artifact.getExtension(),
artifact.getVersion(),
artifact.getProperties(),
artifact.getFile());
result.add(remapped);
} else if ("".equals(artifact.getClassifier())
&& (artifact.getExtension().equals("pom"))
|| artifact.getExtension().startsWith("pom.")) {
// skip POM and POM subordinates
continue;
} else {
// everything else: add as is
result.add(artifact);
}
}
return result;
}
/**
* Consumer POM is transformed from original POM.
*/
private static class ConsumerPomArtifact extends TransformedArtifact {
private ConsumerPomArtifact(MavenProject mavenProject, Path target, RepositorySystemSession session) {
super(
new ProjectArtifact(mavenProject),
() -> mavenProject.getFile().toPath(),
CONSUMER_POM_CLASSIFIER,
"pom",
target,
transformer(session));
}
private static BiConsumer<Path, Path> transformer(RepositorySystemSession session) {
TransformerContext context = (TransformerContext) session.getData().get(TransformerContext.KEY);
return (src, dest) -> {
try (InputStream inputStream = transform(src, context)) {
Files.createDirectories(dest.getParent());
Files.copy(inputStream, dest, StandardCopyOption.REPLACE_EXISTING);
} catch (XmlPullParserException | IOException e) {
throw new RuntimeException(e);
}
};
}
}
/**
* The actual transformation: visible for testing.
*/
static InputStream transform(Path pomFile, TransformerContext context) throws IOException, XmlPullParserException {
XmlStreamReader reader = ReaderFactory.newXmlReader(Files.newInputStream(pomFile));
XmlPullParser parser = new MXParser(EntityReplacementMap.defaultEntityReplacementMap);
parser.setInput(reader);
parser = new RawToConsumerPomXMLFilterFactory(new DefaultBuildPomXMLFilterFactory(context, true))
.get(parser, pomFile);
return XmlUtils.writeDocument(reader, parser);
}
}