From 5782054dec37208b544c7220fe09785e3c7d65ca Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Tue, 6 Sep 2022 22:27:42 +0200 Subject: [PATCH] Use NIO Path API for more presice path handling and checks --- .../tycho/pomless/AbstractTychoMapping.java | 98 +++++++++++-------- .../pomless/AbstractXMLTychoMapping.java | 8 +- .../tycho/pomless/TychoAggregatorMapping.java | 39 ++++---- .../tycho/pomless/TychoBundleMapping.java | 44 +++++---- .../tycho/pomless/TychoFeatureMapping.java | 9 +- .../tycho/pomless/TychoRepositoryMapping.java | 24 ++--- .../tycho/pomless/TychoTargetMapping.java | 9 +- 7 files changed, 129 insertions(+), 102 deletions(-) diff --git a/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/AbstractTychoMapping.java b/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/AbstractTychoMapping.java index 2d8cd801b6..c440457507 100644 --- a/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/AbstractTychoMapping.java +++ b/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/AbstractTychoMapping.java @@ -15,7 +15,6 @@ package org.eclipse.tycho.pomless; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -24,9 +23,13 @@ import java.io.StringReader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.InvalidPathException; +import java.nio.file.Path; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Properties; import java.util.function.Supplier; @@ -83,12 +86,12 @@ public File locatePom(File dir) { @Override public boolean accept(Map options) { - String location = PolyglotModelUtil.getLocation(options); - if (location == null || location.endsWith("pom.xml")) { + Optional location = getLocation(options); + if (location.isEmpty() || getFileName(location.get()).equals("pom.xml")) { //we want that pom.xml takes precedence over our generated one return false; } - return isValidLocation(location); + return isValidLocation(location.get()); } @Override @@ -111,21 +114,20 @@ public ModelWriter getWriter() { @Override public Model read(InputStream input, Map options) throws IOException { - String location = PolyglotModelUtil.getLocation(options); - File file = new File(location); + Path file = getLocation(options).orElseThrow(() -> new IOException("Failed to obtain file location")); try (InputStreamReader stream = new InputStreamReader(input, getPrimaryArtifactCharset())) { return read(stream, file, options); } } @Override - public Model read(File input, Map options) throws IOException { - File artifactFile = getRealArtifactFile(input); - if (artifactFile.isDirectory()) { + public Model read(File inputFile, Map options) throws IOException { + Path input = inputFile.toPath(); + Path artifactFile = getRealArtifactFile(input); + if (Files.isDirectory(artifactFile)) { return read(new StringReader(""), input, options); - } else if (artifactFile.isFile()) { - try (InputStreamReader stream = new InputStreamReader(new FileInputStream(artifactFile), - getPrimaryArtifactCharset())) { + } else if (Files.isRegularFile(artifactFile)) { + try (Reader stream = Files.newBufferedReader(artifactFile, getPrimaryArtifactCharset())) { return read(stream, input, options); } } else { @@ -136,17 +138,17 @@ public Model read(File input, Map options) throws IOException { @Override public Model read(Reader input, Map options) throws IOException { - File file = new File(PolyglotModelUtil.getLocation(options)); + Path file = getLocation(options).orElseThrow(() -> new IOException("Failed to obtain file location")); return read(input, file, options); } - private Model read(Reader artifactReader, File artifactFile, Map options) throws IOException { + private Model read(Reader artifactReader, Path artifactFile, Map options) throws IOException { Model model = new Model(); model.setModelVersion("4.0.0"); model.setPackaging(getPackaging()); initModel(model, artifactReader, artifactFile); if (model.getParent() == null) { - model.setParent(findParent(artifactFile.getParentFile(), options)); + model.setParent(findParent(artifactFile.getParent(), options)); } if (model.getVersion() == null) { //inherit version from parent if not given @@ -159,11 +161,11 @@ private Model read(Reader artifactReader, File artifactFile, Map opti return model; } - protected File getRealArtifactFile(File polyglotArtifactFile) { + protected Path getRealArtifactFile(Path polyglotArtifactFile) { return polyglotArtifactFile; } - protected Parent findParent(File projectRoot, Map projectOptions) throws IOException { + protected Parent findParent(Path projectRoot, Map projectOptions) throws IOException { Parent parent = (Parent) projectOptions.get(MODEL_PARENT); if (parent != null) { //if the parent is given by the options we don't need to search it! @@ -172,17 +174,17 @@ protected Parent findParent(File projectRoot, Map projectOptions) thr Properties buildProperties = getBuildProperties(projectRoot); // assumption parent pom must be physically located in parent directory if not given by build.properties String parentRef = buildProperties.getProperty(TYCHO_POMLESS_PARENT_PROPERTY, PARENT_POM_DEFAULT_VALUE); - File fileOrFolder = new File(projectRoot, parentRef).getCanonicalFile(); + Path fileOrFolder = projectRoot.resolve(parentRef).toRealPath(); PomReference parentPom; - if (fileOrFolder.isFile()) { - parentPom = locatePomReference(fileOrFolder.getParentFile(), fileOrFolder.getName()); - } else if (fileOrFolder.isDirectory()) { + if (Files.isRegularFile(fileOrFolder)) { + parentPom = locatePomReference(fileOrFolder.getParent(), getFileName(fileOrFolder)); + } else if (Files.isDirectory(fileOrFolder)) { parentPom = locatePomReference(fileOrFolder, null); } else { throw new FileNotFoundException("parent pom file/folder " + fileOrFolder + " is not accessible"); } if (parentPom == null) { - throw new FileNotFoundException("No parent pom file found in " + fileOrFolder.getCanonicalPath()); + throw new FileNotFoundException("No parent pom file found in " + fileOrFolder.toRealPath()); } Map options = new HashMap<>(1); options.put(ModelProcessor.SOURCE, new FileModelSource(parentPom.getPomFile())); @@ -201,8 +203,8 @@ protected Parent findParent(File projectRoot, Map projectOptions) thr version = parentModel.getParent().getVersion(); } parentReference.setVersion(version); - parentReference.setRelativePath( - projectRoot.getCanonicalFile().toPath().relativize(parentPom.getPomFile().toPath()).toString()); + parentReference + .setRelativePath(projectRoot.toRealPath().relativize(parentPom.getPomFile().toPath()).toString()); logger.debug("Derived parent for path " + projectRoot + " is groupId: " + parentReference.getGroupId() + ", artifactId: " + parentReference.getArtifactId() + ", relativePath: " + parentReference.getRelativePath()); @@ -218,10 +220,12 @@ protected Parent findParent(File projectRoot, Map projectOptions) thr * the name hint to use * @return the {@link PomReference} or null */ - protected PomReference locatePomReference(File folder, String nameHint) { + protected PomReference locatePomReference(Path folderPath, String nameHint) { + File folder = folderPath.toFile(); PomReference reference = null; try { List lookupList = container.lookupList(ModelProcessor.class); + for (ModelProcessor processor : lookupList) { File pom = processor.locatePom(folder); if (pom != null && (reference == null || pom.getName().equals(nameHint)) && pom.exists()) { @@ -238,7 +242,7 @@ public String getFlavour() { return getPackaging(); } - protected abstract boolean isValidLocation(String location); + protected abstract boolean isValidLocation(Path location); protected abstract File getPrimaryArtifact(File dir); @@ -254,23 +258,23 @@ protected Charset getPrimaryArtifactCharset() { return StandardCharsets.UTF_8; } - protected abstract void initModel(Model model, Reader artifactReader, File artifactFile) throws IOException; + protected abstract void initModel(Model model, Reader artifactReader, Path artifactFile) throws IOException; - protected static Properties getBuildProperties(File dir) throws IOException { - return loadProperties(new File(dir, "build.properties")); + protected static Properties getBuildProperties(Path dir) throws IOException { + return loadProperties(dir.resolve("build.properties")); } - static Properties loadProperties(File propertiesPath) throws IOException { + static Properties loadProperties(Path propertiesPath) throws IOException { Properties properties = new Properties(); - if (propertiesPath.isFile()) { - try (FileInputStream stream = new FileInputStream(propertiesPath)) { + if (Files.isRegularFile(propertiesPath)) { + try (InputStream stream = Files.newInputStream(propertiesPath)) { properties.load(stream); } } return properties; } - static Supplier getPropertiesSupplier(File propertiesFile) { + static Supplier getPropertiesSupplier(Path propertiesFile) { return new Supplier<>() { private Properties properties; @@ -299,22 +303,23 @@ static String localizedValue(String value, Supplier properties) { @Override public Properties getEnhancementProperties(Map options) { - String location = PolyglotModelUtil.getLocation(options); - File file = new File(location); + Optional file = getLocation(options); try { - return getEnhancementProperties(file); + if (file.isPresent()) { + return getEnhancementProperties(file.get()); + } } catch (IOException e) { logger.warn("reading EnhancementProperties encountered a problem and was skipped for this reason", e); } return null; } - protected Properties getEnhancementProperties(File file) throws IOException { - File dir = file.isDirectory() ? file : file.getParentFile(); + protected Properties getEnhancementProperties(Path file) throws IOException { + Path dir = Files.isDirectory(file) ? file : file.getParent(); return getBuildProperties(dir); } - private static void setLocation(Model model, File modelSource) { + private static void setLocation(Model model, Path modelSource) { InputSource inputSource = new InputSource(); inputSource.setLocation(modelSource.toString()); inputSource.setModelId(model.getParent().getGroupId() + ":" + model.getArtifactId() + ":" + model.getVersion()); @@ -349,4 +354,19 @@ public void setMultiModuleProjectDirectory(File multiModuleProjectDirectory) { public void setSnapshotFormat(String snapshotFormat) { this.snapshotFormat = snapshotFormat; } + + static Optional getLocation(Map options) { + String location = PolyglotModelUtil.getLocation(options); + if (location != null) { + try { + return Optional.of(Path.of(location)); + } catch (InvalidPathException e) { + } + } + return Optional.empty(); + } + + static String getFileName(Path file) { + return file.getFileName().toString(); + } } diff --git a/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/AbstractXMLTychoMapping.java b/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/AbstractXMLTychoMapping.java index aa03d5f72d..a59a6972eb 100644 --- a/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/AbstractXMLTychoMapping.java +++ b/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/AbstractXMLTychoMapping.java @@ -44,11 +44,11 @@ public abstract class AbstractXMLTychoMapping extends AbstractTychoMapping { private static final DocumentBuilderFactory FACTORY = DocumentBuilderFactory.newInstance(); @Override - protected void initModel(Model model, Reader artifactReader, File artifactFile) throws IOException { - initModelFromXML(model, parseXML(artifactReader, artifactFile.toURI().toASCIIString()), artifactFile); + protected void initModel(Model model, Reader artifactReader, Path artifactFile) throws IOException { + initModelFromXML(model, parseXML(artifactReader, artifactFile.toUri().toASCIIString()), artifactFile); } - protected abstract void initModelFromXML(Model model, Element xml, File artifactFile) throws IOException; + protected abstract void initModelFromXML(Model model, Element xml, Path artifactFile) throws IOException; protected static Element parseXML(Reader artifactReader, String documentURI) throws IOException { try { @@ -102,7 +102,7 @@ public float getPriority() { protected static Stream filesWithExtension(Path directory, String extension) throws IOException { Predicate nameFilter = n -> !n.startsWith(".polyglot.") && n.endsWith(extension); return Files.walk(directory, 1) // - .filter(p -> nameFilter.test(p.getFileName().toString())) // + .filter(p -> nameFilter.test(getFileName(p))) // .filter(Files::isRegularFile).map(Path::toFile); } } diff --git a/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoAggregatorMapping.java b/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoAggregatorMapping.java index c827798de9..af5b79560d 100644 --- a/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoAggregatorMapping.java +++ b/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoAggregatorMapping.java @@ -19,10 +19,10 @@ import java.io.BufferedReader; import java.io.File; -import java.io.FileFilter; import java.io.IOException; import java.io.Reader; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Iterator; import java.util.Set; import java.util.TreeSet; @@ -57,8 +57,8 @@ public float getPriority() { } @Override - protected boolean isValidLocation(String location) { - return location.endsWith(TYCHO_POM); + protected boolean isValidLocation(Path location) { + return getFileName(location).equals(TYCHO_POM); } @Override @@ -69,28 +69,29 @@ protected File getPrimaryArtifact(File dir) { } if (COMMON_NAMES.contains(dir.getName().toLowerCase())) { logger.debug("Scanning folder " + dir + " for modules"); - File[] subFolders = dir.listFiles((FileFilter) File::isDirectory); - if (subFolders != null) { - Set modules = new TreeSet<>(); - for (File subfolder : subFolders) { + Set modules = new TreeSet<>(); + try (var subFolders = Files.newDirectoryStream(dir.toPath(), Files::isDirectory)) { + for (Path subfolder : subFolders) { PomReference reference = locatePomReference(subfolder, null); if (reference != null) { - String name = subfolder.getName(); + String name = getFileName(subfolder); modules.add(name); logger.debug("Found pom " + reference.getPomFile().getName() + " in subfolder " + name); } } - if (!modules.isEmpty()) { - file.deleteOnExit(); - Stream lines = concat(of(TYCHO_AUTOMATIC_GENERATED_FILE_HEADER), modules.stream()); - try { - Files.write(file.toPath(), lines::iterator, getPrimaryArtifactCharset()); - } catch (IOException e) { - throw new RuntimeException("writing modules file failed", e); - } - return file; + } catch (IOException e) { // assume empty + } + if (!modules.isEmpty()) { + file.deleteOnExit(); + Stream lines = concat(of(TYCHO_AUTOMATIC_GENERATED_FILE_HEADER), modules.stream()); + try { + Files.write(file.toPath(), lines::iterator, getPrimaryArtifactCharset()); + } catch (IOException e) { + throw new RuntimeException("writing modules file failed", e); } + return file; } + } else { logger.debug("Skip folder " + dir + " because it does not match any common name " + COMMON_NAMES); } @@ -98,7 +99,7 @@ protected File getPrimaryArtifact(File dir) { } @Override - protected void initModel(Model model, Reader artifactReader, File artifactFile) throws IOException { + protected void initModel(Model model, Reader artifactReader, Path artifactFile) throws IOException { logger.debug("Generate aggregator pom for " + artifactFile); try (BufferedReader reader = new BufferedReader(artifactReader)) { Stream lines = reader.lines().filter(l -> !l.startsWith("#") && !l.isBlank()).map(String::strip); @@ -107,7 +108,7 @@ protected void initModel(Model model, Reader artifactReader, File artifactFile) logger.debug("Adding module " + line); model.getModules().add(line); } - model.setArtifactId(artifactFile.getParentFile().getName()); + model.setArtifactId(getFileName(artifactFile.getParent())); model.setName("[aggregator] " + model.getArtifactId()); } } diff --git a/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoBundleMapping.java b/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoBundleMapping.java index 240c48a655..705b66b2c2 100644 --- a/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoBundleMapping.java +++ b/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoBundleMapping.java @@ -15,9 +15,11 @@ package org.eclipse.tycho.pomless; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.io.Reader; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; import java.util.Properties; import java.util.function.Supplier; @@ -57,9 +59,9 @@ public float getPriority() { } @Override - protected boolean isValidLocation(String location) { - File polyglotFile = new File(location); - return polyglotFile.getName().equals(META_INF_DIRECTORY) && new File(polyglotFile, MANIFEST_MF).isFile(); + protected boolean isValidLocation(Path polyglotFile) { + return getFileName(polyglotFile).equals(META_INF_DIRECTORY) + && Files.isRegularFile(polyglotFile.resolve(MANIFEST_MF)); } @Override @@ -72,9 +74,9 @@ protected File getPrimaryArtifact(File dir) { } @Override - protected void initModel(Model model, Reader artifactReader, File artifactFile) throws IOException { - File bundleRoot = artifactFile.getParentFile(); - File manifestFile = new File(artifactFile, MANIFEST_MF); + protected void initModel(Model model, Reader artifactReader, Path artifactFile) throws IOException { + Path bundleRoot = artifactFile.getParent(); + Path manifestFile = artifactFile.resolve(MANIFEST_MF); Attributes manifestHeaders = readManifestHeaders(manifestFile); String bundleSymbolicName = getBundleSymbolicName(manifestHeaders, manifestFile); // groupId is inherited from parent pom @@ -88,7 +90,7 @@ protected void initModel(Model model, Reader artifactReader, File artifactFile) } else { prefix = NAME_PREFIX; } - File l10nFile = getBundleLocalizationPropertiesFile(manifestHeaders, manifestFile); + Path l10nFile = getBundleLocalizationPropertiesFile(manifestHeaders, manifestFile); Supplier properties = getPropertiesSupplier(l10nFile); String bundleName = getManifestAttributeValue(manifestHeaders, "Bundle-Name", properties); @@ -104,18 +106,18 @@ protected void initModel(Model model, Reader artifactReader, File artifactFile) if (description != null) { model.setDescription(description); } - File bndFile = new File(bundleRoot, "bnd.bnd"); - if (bndFile.exists()) { + Path bndFile = bundleRoot.resolve("bnd.bnd"); + if (Files.isRegularFile(bndFile)) { createBndPlugin(model); } } @Override - protected Properties getEnhancementProperties(File file) throws IOException { - if (file.getName().equals(META_INF_DIRECTORY) && file.isDirectory()) { + protected Properties getEnhancementProperties(Path file) throws IOException { + if (getFileName(file).equals(META_INF_DIRECTORY) && Files.isDirectory(file)) { //Look up build.properties in the project's root. The given file points to the 'META-INF' folder. - return getBuildProperties(file.getParentFile()); + return getBuildProperties(file.getParent()); } else { return super.getEnhancementProperties(file); } @@ -146,15 +148,15 @@ private static Plugin createBndPlugin(Model model) { return plugin; } - private Attributes readManifestHeaders(File manifestFile) throws IOException { + private Attributes readManifestHeaders(Path manifestFile) throws IOException { Manifest manifest = new Manifest(); - try (FileInputStream stream = new FileInputStream(manifestFile)) { + try (InputStream stream = Files.newInputStream(manifestFile)) { manifest.read(stream); } return manifest.getMainAttributes(); } - private String getBundleSymbolicName(Attributes headers, File manifestFile) throws ModelParseException { + private String getBundleSymbolicName(Attributes headers, Path manifestFile) throws ModelParseException { String symbolicName = getRequiredHeaderValue(BUNDLE_SYMBOLIC_NAME, headers, manifestFile); // strip off any directives/attributes int semicolonIndex = symbolicName.indexOf(';'); @@ -164,7 +166,7 @@ private String getBundleSymbolicName(Attributes headers, File manifestFile) thro return symbolicName; } - private String getRequiredHeaderValue(String headerKey, Attributes headers, File manifestFile) + private String getRequiredHeaderValue(String headerKey, Attributes headers, Path manifestFile) throws ModelParseException { String value = headers.getValue(headerKey); if (value == null) { @@ -173,7 +175,7 @@ private String getRequiredHeaderValue(String headerKey, Attributes headers, File return value; } - private boolean isTestBundle(String bundleSymbolicName, File bundleRoot) throws IOException { + private boolean isTestBundle(String bundleSymbolicName, Path bundleRoot) throws IOException { Properties buildProperties = getBuildProperties(bundleRoot); // Although user defined packaging-type takes precedence, check it to have a corresponding artifactId String packagingProperty = buildProperties.getProperty(PACKAGING_PROPERTY); @@ -188,20 +190,20 @@ private boolean isTestBundle(String bundleSymbolicName, File bundleRoot) throws } private static String getManifestAttributeValue(Attributes headers, String attributeName, - Supplier localizationProperties) throws IOException { + Supplier localizationProperties) { String rawValue = headers.getValue(attributeName); String localizedValue = localizedValue(rawValue, localizationProperties); return localizedValue != null && !localizedValue.isBlank() ? localizedValue : null; } - private static File getBundleLocalizationPropertiesFile(Attributes headers, File manifestFile) { + private static Path getBundleLocalizationPropertiesFile(Attributes headers, Path manifestFile) { String location = headers.getValue("Bundle-Localization"); if (location == null || location.isEmpty()) { location = "OSGI-INF/l10n/bundle"; } //we always use the default here to have consistent build regardless of locale settings - return new File(manifestFile.getParentFile().getParentFile(), location + ".properties"); + return manifestFile.getParent().getParent().resolve(location + ".properties"); } } diff --git a/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoFeatureMapping.java b/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoFeatureMapping.java index dec434fadf..6423015f8a 100644 --- a/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoFeatureMapping.java +++ b/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoFeatureMapping.java @@ -17,6 +17,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.util.Properties; import java.util.function.Supplier; @@ -44,11 +45,11 @@ public float getPriority() { } @Override - protected void initModelFromXML(Model model, Element xml, File artifactFile) throws IOException { + protected void initModelFromXML(Model model, Element xml, Path artifactFile) throws IOException { model.setArtifactId(getRequiredXMLAttributeValue(xml, "id")); model.setVersion(getPomVersion(getRequiredXMLAttributeValue(xml, "version"))); - File featureProperties = new File(artifactFile.getParentFile(), "feature.properties"); + Path featureProperties = artifactFile.getParent().resolve("feature.properties"); Supplier properties = getPropertiesSupplier(featureProperties); String label = localizedValue(getXMLAttributeValue(xml, "label"), properties); @@ -63,8 +64,8 @@ protected void initModelFromXML(Model model, Element xml, File artifactFile) thr } @Override - protected boolean isValidLocation(String location) { - return location.endsWith(FEATURE_XML); + protected boolean isValidLocation(Path location) { + return getFileName(location).equals(FEATURE_XML); } @Override diff --git a/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoRepositoryMapping.java b/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoRepositoryMapping.java index 516de3b476..6e2719501c 100644 --- a/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoRepositoryMapping.java +++ b/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoRepositoryMapping.java @@ -19,6 +19,7 @@ import java.io.FileReader; import java.io.IOException; import java.io.Reader; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -66,14 +67,14 @@ public float getPriority() { } @Override - protected void initModel(Model model, Reader artifactReader, File artifactFile) throws IOException { - if (artifactFile.getName().endsWith(PRODUCT_EXTENSION)) { - File projectRoot = artifactFile.getParentFile(); - try (var files = filesWithExtension(projectRoot.toPath(), PRODUCT_EXTENSION)) { + protected void initModel(Model model, Reader artifactReader, Path artifactFile) throws IOException { + if (getFileName(artifactFile).endsWith(PRODUCT_EXTENSION)) { + Path projectRoot = artifactFile.getParent(); + try (var files = filesWithExtension(projectRoot, PRODUCT_EXTENSION)) { List products = files.collect(Collectors.toList()); if (products.size() > 1) { //multiple products must inherit version from parent but get the artifact-id from the parent-folder - model.setArtifactId(projectRoot.getName()); + model.setArtifactId(getFileName(projectRoot)); Plugin directorPlugin = createDirectorPlugin(model); List names = new ArrayList<>(); for (File file : products) { @@ -95,8 +96,8 @@ protected void initModel(Model model, Reader artifactReader, File artifactFile) } @Override - protected void initModelFromXML(Model model, Element xml, File artifactFile) throws IOException { - if (artifactFile.getName().endsWith(PRODUCT_EXTENSION)) { + protected void initModelFromXML(Model model, Element xml, Path artifactFile) throws IOException { + if (getFileName(artifactFile).endsWith(PRODUCT_EXTENSION)) { model.setArtifactId(getRequiredXMLAttributeValue(xml, PRODUCT_UID_ATTRIBUTE)); String version = getXMLAttributeValue(xml, PRODUCT_VERSION_ATTRIBUTE); if (version != null) { @@ -110,8 +111,8 @@ protected void initModelFromXML(Model model, Element xml, File artifactFile) thr } } - private static void initFromCategory(Model model, File categoryXml) { - String name = categoryXml.getParentFile().getName(); + private static void initFromCategory(Model model, Path categoryXml) { + String name = getFileName(categoryXml.getParent()); if (!name.endsWith(UPDATE_SITE_SUFFIX)) { name = name + UPDATE_SITE_SUFFIX; } @@ -120,8 +121,9 @@ private static void initFromCategory(Model model, File categoryXml) { } @Override - protected boolean isValidLocation(String location) { - return location.endsWith(PRODUCT_EXTENSION) || location.endsWith(CATEGORY_XML); + protected boolean isValidLocation(Path location) { + String fileName = getFileName(location); + return fileName.endsWith(PRODUCT_EXTENSION) || fileName.endsWith(CATEGORY_XML); } @Override diff --git a/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoTargetMapping.java b/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoTargetMapping.java index b774493b3d..5ff4665aff 100644 --- a/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoTargetMapping.java +++ b/tycho-extras/tycho-pomless/src/main/java/org/eclipse/tycho/pomless/TychoTargetMapping.java @@ -17,6 +17,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.util.List; import java.util.stream.Collectors; @@ -43,8 +44,8 @@ public float getPriority() { } @Override - protected boolean isValidLocation(String location) { - return location.endsWith(TARGET_EXTENSION); + protected boolean isValidLocation(Path location) { + return getFileName(location).endsWith(TARGET_EXTENSION); } @Override @@ -69,8 +70,8 @@ protected File getPrimaryArtifact(File dir) { } @Override - protected void initModelFromXML(Model model, Element xml, File artifactFile) throws IOException { - String fileName = artifactFile.getName(); + protected void initModelFromXML(Model model, Element xml, Path artifactFile) throws IOException { + String fileName = getFileName(artifactFile); String artifactId = fileName.substring(0, fileName.length() - TARGET_EXTENSION.length()); model.setArtifactId(artifactId); String name = getXMLAttributeValue(xml, "name");