-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Maven4 "legacy" mode and more #2380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
7128e95
e664445
0c69e92
ceb2528
2e16d72
e6ca248
f24faf6
121c95c
5e63387
d501096
2c4c5d5
8d8f12c
6bfa6c6
c1b17f3
30e3475
83bf9ad
d02447e
308f822
b007d89
1179f61
c57c8e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,15 +23,16 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||
| import javax.xml.stream.XMLStreamException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.io.IOException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.nio.file.Files; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.nio.file.Path; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Objects; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.regex.Matcher; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.regex.Pattern; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.maven.api.feature.Features; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.maven.api.services.ModelBuilderException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.maven.internal.transformation.PomArtifactTransformer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.maven.api.model.Model; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.maven.project.MavenProject; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.eclipse.aether.RepositorySystemSession; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.eclipse.aether.deployment.DeployRequest; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.eclipse.aether.installation.InstallRequest; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Inliner POM transformer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -40,28 +41,45 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Singleton | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Named | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| class PomInlinerTransformer implements PomArtifactTransformer { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| public InstallRequest remapInstallArtifacts(RepositorySystemSession session, InstallRequest request) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return request; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| public DeployRequest remapDeployArtifacts(RepositorySystemSession session, DeployRequest request) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return request; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| class PomInlinerTransformer extends TransformerSupport { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| protected static final Pattern PLACEHOLDER_EXPRESSION = Pattern.compile("\\$\\{(.+?)}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void injectTransformedArtifacts(RepositorySystemSession session, MavenProject currentProject) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| throws IOException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void injectTransformedArtifacts(RepositorySystemSession session, MavenProject project) throws IOException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!Features.consumerPom(session.getConfigProperties())) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Model model = read(project.getFile().toPath()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| String version = model.getVersion(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| String newVersion = version; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| int lastEnd = -1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (version != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Matcher m = PLACEHOLDER_EXPRESSION.matcher(version.trim()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (m.find()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| String property = m.group(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!session.getConfigProperties().containsKey(property)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new IllegalArgumentException("Cannot inline property " + property); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| String propertyValue = | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| (String) session.getConfigProperties().get(property); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (lastEnd < 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| newVersion = version.substring(0, m.start()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| newVersion += version.substring(lastEnd, m.start()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| lastEnd = m.end(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| newVersion += propertyValue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!Objects.equals(version, newVersion)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| model = model.withVersion(newVersion); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Path tmpPom = Files.createTempFile( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| project.getArtifactId() + "-" + project.getVersion() + "-", ".xml"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| write(model, tmpPom); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| project.setFile(tmpPom.toFile()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| Matcher m = PLACEHOLDER_EXPRESSION.matcher(version.trim()); | |
| while (m.find()) { | |
| String property = m.group(1); | |
| if (!session.getConfigProperties().containsKey(property)) { | |
| throw new IllegalArgumentException("Cannot inline property " + property); | |
| } | |
| String propertyValue = | |
| (String) session.getConfigProperties().get(property); | |
| if (lastEnd < 0) { | |
| newVersion = version.substring(0, m.start()); | |
| } else { | |
| newVersion += version.substring(lastEnd, m.start()); | |
| } | |
| lastEnd = m.end(); | |
| newVersion += propertyValue; | |
| } | |
| if (!Objects.equals(version, newVersion)) { | |
| model = model.withVersion(newVersion); | |
| Path tmpPom = Files.createTempFile( | |
| project.getArtifactId() + "-" + project.getVersion() + "-", ".xml"); | |
| write(model, tmpPom); | |
| project.setFile(tmpPom.toFile()); | |
| } | |
| } | |
| handleVersion(session, project, version, -1, version, model); |
could give dedicated attention to this concern. Operating on all the fields is kind of smell also.
private static void handleVersion(RepositorySystemSession session, MavenProject project, String version, int lastEnd, String newVersion, Model model) throws IOException {
Matcher m = PLACEHOLDER_EXPRESSION.matcher(version.trim());
while (m.find()) {
String property = m.group(1);
if (!session.getConfigProperties().containsKey(property)) {
throw new IllegalArgumentException("Cannot inline property " + property);
}
String propertyValue =
(String) session.getConfigProperties().get(property);
if (lastEnd < 0) {
newVersion = version.substring(0, m.start());
} else {
newVersion += version.substring(lastEnd, m.start());
}
lastEnd = m.end();
newVersion += propertyValue;
}
if (!Objects.equals(version, newVersion)) {
model = model.withVersion(newVersion);
Path tmpPom = Files.createTempFile(
project.getArtifactId() + "-" + project.getVersion() + "-", ".xml");
write(model, tmpPom);
project.setFile(tmpPom.toFile());
}
}| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,88 @@ | ||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||
| * 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.impl; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import javax.xml.stream.XMLStreamException; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import java.io.IOException; | ||||||||||||||||||||||||||||||||
| import java.io.InputStream; | ||||||||||||||||||||||||||||||||
| import java.io.Writer; | ||||||||||||||||||||||||||||||||
| import java.nio.file.Files; | ||||||||||||||||||||||||||||||||
| import java.nio.file.Path; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import org.apache.maven.api.model.Model; | ||||||||||||||||||||||||||||||||
| import org.apache.maven.api.services.ModelBuilderException; | ||||||||||||||||||||||||||||||||
| import org.apache.maven.internal.transformation.PomArtifactTransformer; | ||||||||||||||||||||||||||||||||
| import org.apache.maven.model.v4.MavenStaxReader; | ||||||||||||||||||||||||||||||||
| import org.apache.maven.model.v4.MavenStaxWriter; | ||||||||||||||||||||||||||||||||
| import org.apache.maven.project.MavenProject; | ||||||||||||||||||||||||||||||||
| import org.eclipse.aether.RepositorySystemSession; | ||||||||||||||||||||||||||||||||
| import org.eclipse.aether.deployment.DeployRequest; | ||||||||||||||||||||||||||||||||
| import org.eclipse.aether.installation.InstallRequest; | ||||||||||||||||||||||||||||||||
| import org.slf4j.Logger; | ||||||||||||||||||||||||||||||||
| import org.slf4j.LoggerFactory; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * Support class. | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| abstract class TransformerSupport implements PomArtifactTransformer { | ||||||||||||||||||||||||||||||||
| protected final Logger logger = LoggerFactory.getLogger(getClass()); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||
| public InstallRequest remapInstallArtifacts(RepositorySystemSession session, InstallRequest request) { | ||||||||||||||||||||||||||||||||
| return request; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||
| public DeployRequest remapDeployArtifacts(RepositorySystemSession session, DeployRequest request) { | ||||||||||||||||||||||||||||||||
| return request; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||
| public void injectTransformedArtifacts(RepositorySystemSession session, MavenProject project) throws IOException {} | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||
| public void transform(MavenProject project, RepositorySystemSession session, Path src, Path tgt) | ||||||||||||||||||||||||||||||||
| throws ModelBuilderException, XMLStreamException, IOException { | ||||||||||||||||||||||||||||||||
| throw new IllegalStateException("This transformer does not use this call."); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| protected static final String NAMESPACE_FORMAT = "http://maven.apache.org/POM/%s"; | ||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider grouping all fields like we would do for constructors: https://checkstyle.sourceforge.io/checks/coding/constructorsdeclarationgrouping.html |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| protected static final String SCHEMA_LOCATION_FORMAT = "https://maven.apache.org/xsd/maven-%s.xsd"; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| protected Model read(Path src) throws IOException, XMLStreamException { | ||||||||||||||||||||||||||||||||
| MavenStaxReader reader = new MavenStaxReader(); | ||||||||||||||||||||||||||||||||
| try (InputStream is = Files.newInputStream(src)) { | ||||||||||||||||||||||||||||||||
| return reader.read(is, false, null); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| MavenStaxReader reader = new MavenStaxReader(); | |
| try (InputStream is = Files.newInputStream(src)) { | |
| return reader.read(is, false, null); | |
| } | |
| try (InputStream is = Files.newInputStream(src)) { | |
| return new MavenStaxReader().read(is, false, null); | |
| } |
could inline.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| String version = model.getModelVersion(); | |
| Files.createDirectories(dest.getParent()); | |
| try (Writer w = Files.newBufferedWriter(dest)) { | |
| MavenStaxWriter writer = new MavenStaxWriter(); | |
| writer.setNamespace(String.format(NAMESPACE_FORMAT, version)); | |
| writer.setSchemaLocation(String.format(SCHEMA_LOCATION_FORMAT, version)); | |
| writer.setAddLocationInformation(false); | |
| writer.write(w, model); | |
| } | |
| } | |
| Files.createDirectories(dest.getParent()); | |
| try (Writer w = Files.newBufferedWriter(dest)) { | |
| write(model.getModelVersion(), w, new MavenStaxWriter()) | |
| } | |
| } |
could delegate as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, all is kind of implied.