mirror of https://github.com/apache/maven.git
Add ModelLocator#locateExistingPom to abstract a bit more (#1237)
This commit is contained in:
parent
e71d2d7167
commit
46fd5e9586
|
@ -497,15 +497,11 @@ private boolean build(
|
||||||
|
|
||||||
module = module.replace('\\', File.separatorChar).replace('/', File.separatorChar);
|
module = module.replace('\\', File.separatorChar).replace('/', File.separatorChar);
|
||||||
|
|
||||||
File moduleFile = new File(basedir, module);
|
File moduleFile = modelProcessor.locateExistingPom(new File(basedir, module));
|
||||||
|
|
||||||
if (moduleFile.isDirectory()) {
|
if (moduleFile == null) {
|
||||||
moduleFile = modelProcessor.locatePom(moduleFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!moduleFile.isFile()) {
|
|
||||||
ModelProblem problem = new DefaultModelProblem(
|
ModelProblem problem = new DefaultModelProblem(
|
||||||
"Child module " + moduleFile + " of " + pomFile + " does not exist",
|
"Child module " + module + " of " + pomFile + " does not exist",
|
||||||
ModelProblem.Severity.ERROR,
|
ModelProblem.Severity.ERROR,
|
||||||
ModelProblem.Version.BASE,
|
ModelProblem.Version.BASE,
|
||||||
model,
|
model,
|
||||||
|
|
|
@ -97,15 +97,16 @@ public List<MavenProject> collectProjects(MavenExecutionRequest request) throws
|
||||||
}
|
}
|
||||||
|
|
||||||
private File getMultiModuleProjectPomFile(MavenExecutionRequest request) {
|
private File getMultiModuleProjectPomFile(MavenExecutionRequest request) {
|
||||||
if (request.getPom().getParentFile().equals(request.getMultiModuleProjectDirectory())) {
|
File multiModuleProjectDirectory = request.getMultiModuleProjectDirectory();
|
||||||
|
if (request.getPom().getParentFile().equals(multiModuleProjectDirectory)) {
|
||||||
return request.getPom();
|
return request.getPom();
|
||||||
} else {
|
} else {
|
||||||
File multiModuleProjectPom = modelLocator.locatePom(request.getMultiModuleProjectDirectory());
|
File multiModuleProjectPom = modelLocator.locateExistingPom(multiModuleProjectDirectory);
|
||||||
if (!multiModuleProjectPom.exists()) {
|
if (multiModuleProjectPom == null) {
|
||||||
LOGGER.info(
|
LOGGER.info(
|
||||||
"Maven detected that the requested POM file is part of a multi-module project, "
|
"Maven detected that the requested POM file is part of a multi-module project, "
|
||||||
+ "but could not find a pom.xml file in the multi-module root directory '{}'.",
|
+ "but could not find a pom.xml file in the multi-module root directory '{}'.",
|
||||||
request.getMultiModuleProjectDirectory());
|
multiModuleProjectDirectory);
|
||||||
LOGGER.info("The reactor is limited to all projects under: "
|
LOGGER.info("The reactor is limited to all projects under: "
|
||||||
+ request.getPom().getParent());
|
+ request.getPom().getParent());
|
||||||
return request.getPom();
|
return request.getPom();
|
||||||
|
|
|
@ -1354,18 +1354,11 @@ private File determinePom(final CommandLine commandLine, final String workingDir
|
||||||
current = resolveFile(new File(alternatePomFile), workingDirectory);
|
current = resolveFile(new File(alternatePomFile), workingDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
File pom;
|
if (modelProcessor != null) {
|
||||||
if (current.isDirectory() && modelProcessor != null) {
|
return modelProcessor.locateExistingPom(current);
|
||||||
pom = modelProcessor.locatePom(current);
|
|
||||||
} else {
|
} else {
|
||||||
pom = current;
|
return current.isFile() ? current : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pom.isFile()) {
|
|
||||||
return pom;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Visible for testing
|
// Visible for testing
|
||||||
|
|
|
@ -144,6 +144,7 @@ under the License.
|
||||||
<exclude>org.apache.maven.model.interpolation.StringSearchModelInterpolator</exclude>
|
<exclude>org.apache.maven.model.interpolation.StringSearchModelInterpolator</exclude>
|
||||||
<exclude>org.apache.maven.model.interpolation.StringVisitorModelInterpolator</exclude>
|
<exclude>org.apache.maven.model.interpolation.StringVisitorModelInterpolator</exclude>
|
||||||
<exclude>org.apache.maven.model.io.DefaultModelReader#DefaultModelReader():CONSTRUCTOR_REMOVED</exclude>
|
<exclude>org.apache.maven.model.io.DefaultModelReader#DefaultModelReader():CONSTRUCTOR_REMOVED</exclude>
|
||||||
|
<exclude>org.apache.maven.model.locator.ModelLocator#locateExistingPom(java.io.File):METHOD_NEW_DEFAULT</exclude>
|
||||||
<exclude>org.apache.maven.model.management.DefaultDependencyManagementInjector$ManagementModelMerger</exclude>
|
<exclude>org.apache.maven.model.management.DefaultDependencyManagementInjector$ManagementModelMerger</exclude>
|
||||||
<exclude>org.apache.maven.model.management.DefaultPluginManagementInjector$ManagementModelMerger</exclude>
|
<exclude>org.apache.maven.model.management.DefaultPluginManagementInjector$ManagementModelMerger</exclude>
|
||||||
<exclude>org.apache.maven.model.merge.MavenModelMerger</exclude>
|
<exclude>org.apache.maven.model.merge.MavenModelMerger</exclude>
|
||||||
|
|
|
@ -1894,7 +1894,8 @@ public TransformerContext initialize(ModelBuildingRequest request, ModelProblemC
|
||||||
return new TransformerContext() {
|
return new TransformerContext() {
|
||||||
@Override
|
@Override
|
||||||
public Path locate(Path path) {
|
public Path locate(Path path) {
|
||||||
return modelProcessor.locatePom(path.toFile()).toPath();
|
File file = modelProcessor.locateExistingPom(path.toFile());
|
||||||
|
return file != null ? file.toPath() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -26,9 +26,11 @@
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import org.apache.maven.model.Model;
|
import org.apache.maven.building.Source;
|
||||||
import org.apache.maven.model.io.ModelReader;
|
import org.apache.maven.model.io.ModelReader;
|
||||||
import org.apache.maven.model.locator.ModelLocator;
|
import org.apache.maven.model.locator.ModelLocator;
|
||||||
import org.eclipse.sisu.Typed;
|
import org.eclipse.sisu.Typed;
|
||||||
|
@ -63,32 +65,88 @@
|
||||||
@Typed(ModelProcessor.class)
|
@Typed(ModelProcessor.class)
|
||||||
public class DefaultModelProcessor implements ModelProcessor {
|
public class DefaultModelProcessor implements ModelProcessor {
|
||||||
|
|
||||||
private final ModelLocator locator;
|
private final ModelLocator modelLocator;
|
||||||
private final ModelReader reader;
|
private final ModelReader modelReader;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public DefaultModelProcessor(ModelLocator locator, ModelReader reader) {
|
public DefaultModelProcessor(ModelLocator modelLocator, ModelReader modelReader) {
|
||||||
this.locator = locator;
|
this.modelLocator = modelLocator;
|
||||||
this.reader = reader;
|
this.modelReader = modelReader;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File locatePom(File projectDirectory) {
|
public File locatePom(File projectDirectory) {
|
||||||
return locator.locatePom(projectDirectory);
|
return locatePom(projectDirectory.toPath()).toFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Path locatePom(Path projectDirectory) {
|
||||||
|
// Note that the ModelProcessor#locatePom never returns null
|
||||||
|
// while the ModelParser#locatePom needs to return an existing path!
|
||||||
|
Path pom = modelLocator.locatePom(projectDirectory.toFile()).toPath();
|
||||||
|
if (!pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) {
|
||||||
|
throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom);
|
||||||
|
}
|
||||||
|
return pom;
|
||||||
|
}
|
||||||
|
|
||||||
|
public File locateExistingPom(File projectDirectory) {
|
||||||
|
Path path = locateExistingPom(projectDirectory.toPath());
|
||||||
|
return path != null ? path.toFile() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Path locateExistingPom(Path projectDirectory) {
|
||||||
|
// Note that the ModelProcessor#locatePom never returns null
|
||||||
|
File f = modelLocator.locateExistingPom(projectDirectory.toFile());
|
||||||
|
Path pom = f != null ? f.toPath() : null;
|
||||||
|
if (pom != null && !pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) {
|
||||||
|
throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom);
|
||||||
|
}
|
||||||
|
return pom;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected org.apache.maven.api.model.Model read(
|
||||||
|
Path pomFile, InputStream input, Reader reader, Map<String, ?> options) throws IOException {
|
||||||
|
Source source = (Source) options.get(ModelProcessor.SOURCE);
|
||||||
|
if (pomFile == null && source instanceof org.apache.maven.building.FileSource) {
|
||||||
|
pomFile = ((org.apache.maven.building.FileSource) source).getFile().toPath();
|
||||||
|
}
|
||||||
|
return readXmlModel(pomFile, input, reader, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
private org.apache.maven.api.model.Model readXmlModel(
|
||||||
|
Path pomFile, InputStream input, Reader reader, Map<String, ?> options) throws IOException {
|
||||||
|
if (pomFile != null) {
|
||||||
|
return modelReader.read(pomFile.toFile(), options).getDelegate();
|
||||||
|
} else if (input != null) {
|
||||||
|
return modelReader.read(input, options).getDelegate();
|
||||||
|
} else {
|
||||||
|
return modelReader.read(reader, options).getDelegate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Model read(File input, Map<String, ?> options) throws IOException {
|
public org.apache.maven.model.Model read(File file, Map<String, ?> options) throws IOException {
|
||||||
return reader.read(input, options);
|
Objects.requireNonNull(file, "file cannot be null");
|
||||||
|
Path path = file.toPath();
|
||||||
|
org.apache.maven.api.model.Model model = read(path, null, null, options);
|
||||||
|
return new org.apache.maven.model.Model(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Model read(Reader input, Map<String, ?> options) throws IOException {
|
public org.apache.maven.model.Model read(InputStream input, Map<String, ?> options) throws IOException {
|
||||||
return reader.read(input, options);
|
Objects.requireNonNull(input, "input cannot be null");
|
||||||
|
try (InputStream in = input) {
|
||||||
|
org.apache.maven.api.model.Model model = read(null, in, null, options);
|
||||||
|
return new org.apache.maven.model.Model(model);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Model read(InputStream input, Map<String, ?> options) throws IOException {
|
public org.apache.maven.model.Model read(Reader reader, Map<String, ?> options) throws IOException {
|
||||||
return reader.read(input, options);
|
Objects.requireNonNull(reader, "reader cannot be null");
|
||||||
|
try (Reader r = reader) {
|
||||||
|
org.apache.maven.api.model.Model model = read(null, null, r, options);
|
||||||
|
return new org.apache.maven.model.Model(model);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.maven.model.building;
|
package org.apache.maven.model.building;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
@ -100,7 +101,8 @@ public Model getRawModel(String groupId, String artifactId) {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Path locate(Path path) {
|
public Path locate(Path path) {
|
||||||
return modelLocator.locatePom(path.toFile()).toPath();
|
File file = modelLocator.locateExistingPom(path.toFile());
|
||||||
|
return file != null ? file.toPath() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
static class GAKey {
|
static class GAKey {
|
||||||
|
|
|
@ -54,13 +54,11 @@ public File getPomFile() {
|
||||||
public ModelSource3 getRelatedSource(ModelLocator locator, String relPath) {
|
public ModelSource3 getRelatedSource(ModelLocator locator, String relPath) {
|
||||||
relPath = relPath.replace('\\', File.separatorChar).replace('/', File.separatorChar);
|
relPath = relPath.replace('\\', File.separatorChar).replace('/', File.separatorChar);
|
||||||
|
|
||||||
File relatedPom = new File(getFile().getParentFile(), relPath);
|
File path = new File(getFile().getParentFile(), relPath);
|
||||||
|
|
||||||
if (relatedPom.isDirectory() && locator != null) {
|
File relatedPom = locator.locateExistingPom(path);
|
||||||
relatedPom = locator.locatePom(relatedPom);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (relatedPom.isFile() && relatedPom.canRead()) {
|
if (relatedPom != null) {
|
||||||
return new FileModelSource(relatedPom.toPath().normalize().toFile());
|
return new FileModelSource(relatedPom.toPath().normalize().toFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,21 @@ public class DefaultModelLocator implements ModelLocator {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File locatePom(File projectDirectory) {
|
public File locatePom(File projectDirectory) {
|
||||||
return new File(projectDirectory, "pom.xml");
|
return projectDirectory != null ? projectDirectory : new File(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public File locateExistingPom(File project) {
|
||||||
|
if (project == null || project.isDirectory()) {
|
||||||
|
project = locatePom(project);
|
||||||
|
}
|
||||||
|
if (project.isDirectory()) {
|
||||||
|
File pom = new File(project, "pom.xml");
|
||||||
|
return pom.isFile() ? pom : null;
|
||||||
|
} else if (project.isFile()) {
|
||||||
|
return project;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,4 +37,14 @@ public interface ModelLocator {
|
||||||
* @return The path to the (possibly non-existent) POM file, never {@code null}.
|
* @return The path to the (possibly non-existent) POM file, never {@code null}.
|
||||||
*/
|
*/
|
||||||
File locatePom(File projectDirectory);
|
File locatePom(File projectDirectory);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the file containing the pom or null if a pom can not be found at the given file or in the given directory.
|
||||||
|
*/
|
||||||
|
default File locateExistingPom(File project) {
|
||||||
|
if (project == null || project.isDirectory()) {
|
||||||
|
project = locatePom(project);
|
||||||
|
}
|
||||||
|
return project.isFile() ? project : null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,6 @@
|
||||||
|
|
||||||
import javax.xml.stream.XMLStreamReader;
|
import javax.xml.stream.XMLStreamReader;
|
||||||
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -132,12 +131,9 @@ protected void process(List<Event> buffer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Optional<RelativeProject> resolveRelativePath(Path relativePath, String groupId, String artifactId) {
|
protected Optional<RelativeProject> resolveRelativePath(Path relativePath, String groupId, String artifactId) {
|
||||||
Path pomPath = projectPath.resolve(relativePath).normalize();
|
Path pomPath = modelLocator.apply(projectPath.resolve(relativePath).normalize());
|
||||||
if (Files.isDirectory(pomPath) && modelLocator != null) {
|
|
||||||
pomPath = modelLocator.apply(pomPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pomPath == null || !Files.isRegularFile(pomPath)) {
|
if (pomPath == null) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue