Skip to content

Commit

Permalink
Merge branch 'master' into jt-215-244
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrpav authored Aug 14, 2023
2 parents 824c7d6 + 47738a3 commit 24a1bc4
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -792,13 +792,14 @@ protected void logConfiguration() throws MojoExecutionException {
getLog().info("dependsURIs (resolved):" + getDependsURIs());
}

private void collectBindingUrisFromDependencies(List<URI> bindingUris) throws MojoExecutionException {
void collectBindingUrisFromDependencies(List<URI> bindingUris) throws MojoExecutionException {
@SuppressWarnings("unchecked")
final Collection<Artifact> projectArtifacts = getProject().getArtifacts();
final List<Artifact> compileScopeArtifacts = new ArrayList<Artifact>(projectArtifacts.size());
final ArtifactFilter filter = new ScopeArtifactFilter(DefaultArtifact.SCOPE_COMPILE);
final ArtifactFilter scopeFilter = new ScopeArtifactFilter(DefaultArtifact.SCOPE_COMPILE);
final ArtifactFilter typeFilter = new TypeArtifactFilter("pom");
for (Artifact artifact : projectArtifacts) {
if (filter.include(artifact)) {
if (scopeFilter.include(artifact) && !typeFilter.include(artifact)) {
compileScopeArtifacts.add(artifact);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.MatchPatterns;
import org.codehaus.plexus.util.Scanner;
import org.codehaus.plexus.util.SelectorUtils;
import org.jvnet.jaxb.maven.util.CollectionUtils.Function;
import org.sonatype.plexus.build.incremental.BuildContext;
import org.xml.sax.InputSource;
Expand Down Expand Up @@ -95,11 +98,93 @@ public static List<File> scanDirectoryForFiles(BuildContext buildContext, final

scanner.scan();

final List<File> files = new ArrayList<File>();
for (final String name : scanner.getIncludedFiles()) {
// Reorder files according to includes specific file ordering
List<String> orderedIncludedFiles = reorderFiles(scanner.getIncludedFiles(), includes);

final List<File> files = new ArrayList<>();
for (final String name : orderedIncludedFiles) {
files.add(new File(directory, name).getCanonicalFile());
}

return files;
}

private static boolean isWildcard (final String s) {
return s.indexOf ('*') >= 0 || s.indexOf ('?') >= 0;
}

/**
* Reorder the result of "scanner.getIncludedFile" so that the order of the
* source includes is maintained as good as possible.
* Examples:<br>
* If the includes contain [a, b, c] and the resulting list should be in that order.<br>
* If the includes contain [a, b*, c] the resulting list should be [a, matches-of(b*), c]
*
* @param resolvedFiles
* resolved files from scanner.getIncludedFiles. May not be <code>null</code>.
* @param includes
* The source includes in the correct order. May be <code>null</code> or empty.
* @return The ordered list of files, that tries to take the source order as good as possible
*/
protected static List<String> reorderFiles(final String[] resolvedFiles, final String[] includes) {
if (includes == null || includes.length == 0) {
// return "as is"
return Arrays.asList(resolvedFiles);
}

// copy of the initial list in order to avoid changes in resolvedFiles var
List<String> resolvedFilesList = new ArrayList<>(Arrays.asList(resolvedFiles));
final List<String> ret = new ArrayList<>(resolvedFilesList.size());

// For each include
for (final String include : includes) {
// Create MatchPatterns from normalizePattern of the include (like it was done in DirectoryScanner)
MatchPatterns inc = MatchPatterns.from(normalizePattern(include));

// Find all matches in the resolved files list
final List<String> matches = new ArrayList<>();
for (final String resFile : resolvedFilesList) {
if (inc.matches(resFile, false)) {
matches.add(resFile);
}
}

// Add all matches to the result list
ret.addAll(matches);
// Remove from the main list
resolvedFilesList.removeAll(matches);
}

// Add all remaining resolved files in the order "as is"
ret.addAll(resolvedFilesList);

return ret;
}

/**
* Copy of Plexus-utils function (private) to normalize pattern of include.<br>
* Normalizes the pattern, e.g. converts forward and backward slashes to the platform-specific file separator.
*
* @param pattern The pattern to normalize, must not be <code>null</code>.
* @return The normalized pattern, never <code>null</code>.
*/
private static String normalizePattern(String pattern) {
pattern = pattern.trim();

if (pattern.startsWith(SelectorUtils.REGEX_HANDLER_PREFIX)) {
if (File.separatorChar == '\\') {
pattern = org.codehaus.plexus.util.StringUtils.replace(pattern, "/", "\\\\");
} else {
pattern = org.codehaus.plexus.util.StringUtils.replace(pattern, "\\\\", "/");
}
} else {
pattern = pattern.replace(File.separatorChar == '/' ? '\\' : '/', File.separatorChar);

if (pattern.endsWith(File.separator)) {
pattern += "**";
}
}

return pattern;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.jvnet.jaxb.maven;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.testing.stubs.ArtifactStub;
import org.apache.maven.project.MavenProject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -15,7 +18,9 @@
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

Expand Down Expand Up @@ -43,6 +48,62 @@ public void createJarFile() throws Exception {
}
}

@Test
public void collectBindingUrisFromDependencies() throws Exception {
List<URI> bindings = new ArrayList<>();

final RawXJC2Mojo<Void> mojo = new RawXJC2Mojo<Void>() {

@Override
public MavenProject getProject() {
MavenProject project = new MavenProject() {
@Override
public Set getArtifacts() {
Set<Artifact> artifacts = new HashSet<>();
ArtifactStub stubJar = new ArtifactStub();
stubJar.setArtifactId("test");
stubJar.setScope("compile");
stubJar.setType("jar");
stubJar.setFile(testJarFile);
artifacts.add(stubJar);

ArtifactStub stubProvided = new ArtifactStub();
stubProvided.setArtifactId("test-provided");
stubProvided.setScope("provided");
stubProvided.setType("jar");
artifacts.add(stubJar);

ArtifactStub stubPom = new ArtifactStub();
stubPom.setArtifactId("test-pom");
stubPom.setScope("compile");
stubPom.setType("pom");
artifacts.add(stubPom);
return artifacts;
}
};
return project;
}

@Override
protected IOptionsFactory<Void> getOptionsFactory() {
throw new UnsupportedOperationException();
}

@Override
public void doExecute(Void options) throws MojoExecutionException {
throw new UnsupportedOperationException();
}
};

mojo.collectBindingUrisFromDependencies(bindings);

assertEquals(2, bindings.size());
assertEquals(URI.create("jar:" + testJarFile.toURI() + "!/dir/nested.xjb"), bindings.get(0));
assertEquals(URI.create("jar:" + testJarFile.toURI() + "!/root.xjb"), bindings.get(1));
assertEquals("nested binding", readContent(bindings.get(0)));
assertEquals("root binding", readContent(bindings.get(1)));
}

@Test
public void collectsBindingUrisFromArtifact() throws Exception {
List<URI> bindings = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package org.jvnet.jaxb.maven.util;

import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class IOUtilsTests {

@Test
public void reorderFilesIncludesNull() {
String[] files = {"a.xsd", "c.xsd", "b.xsd" };
List<String> orderedFiles = IOUtils.reorderFiles(files, null);

Assert.assertNotNull("Ordered files list should not be null",
orderedFiles);
Assert.assertEquals("Ordered files list should contains all elements of initial list",
files.length, orderedFiles.size());
Assert.assertEquals(files[0], orderedFiles.get(0));
Assert.assertEquals(files[1], orderedFiles.get(1));
Assert.assertEquals(files[2], orderedFiles.get(2));
}

@Test
public void reorderFilesIncludesEmpty() {
String[] files = {"a.xsd", "c.xsd", "b.xsd" };
List<String> orderedFiles = IOUtils.reorderFiles(files, new String[] {});

Assert.assertNotNull("Ordered files list should not be null",
orderedFiles);
Assert.assertEquals("Ordered files list should contains all elements of initial list",
files.length, orderedFiles.size());
Assert.assertEquals(files[0], orderedFiles.get(0));
Assert.assertEquals(files[1], orderedFiles.get(1));
Assert.assertEquals(files[2], orderedFiles.get(2));
}

@Test
public void reorderFilesIncludesNoWildcard() {
String[] files = {"a.xsd", "c.xsd", "b.xsd" };
List<String> orderedFiles = IOUtils.reorderFiles(files, new String[] { "b.xsd", "c.xsd", "a.xsd" });

Assert.assertNotNull("Ordered files list should not be null",
orderedFiles);
Assert.assertEquals("Ordered files list should contains all elements of initial list",
files.length, orderedFiles.size());
Assert.assertEquals(files[2], orderedFiles.get(0));
Assert.assertEquals(files[1], orderedFiles.get(1));
Assert.assertEquals(files[0], orderedFiles.get(2));
}

@Test
public void reorderFilesIncludesNoWildcardWithCommonSuffix() {
String[] files = {"a.xsd", "b.xsd", "service-ab.xsd" };
List<String> orderedFiles = IOUtils.reorderFiles(files, new String[] { "b.xsd", "a.xsd", "service-ab.xsd" });

Assert.assertNotNull("Ordered files list should not be null",
orderedFiles);
Assert.assertEquals("Ordered files list should contains all elements of initial list",
files.length, orderedFiles.size());
Assert.assertEquals(files[1], orderedFiles.get(0));
Assert.assertEquals(files[0], orderedFiles.get(1));
Assert.assertEquals(files[2], orderedFiles.get(2));
}

@Test
public void reorderFilesIncludesWithWildcardFirst() {
String[] files = {"a.xsd", "common/c1.xsd", "b.xsd", "common/c2.xsd", "common/a.xsd", "common/b.xsd" };
List<String> orderedFiles = IOUtils.reorderFiles(files, new String[] { "common/*.xsd", "a.xsd", "b.xsd" });

Assert.assertNotNull("Ordered files list should not be null",
orderedFiles);
Assert.assertEquals("Ordered files list should contains all elements of initial list",
files.length, orderedFiles.size());
// we have all common/*.xsd files in same order
Assert.assertEquals(files[1], orderedFiles.get(0));
Assert.assertEquals(files[3], orderedFiles.get(1));
Assert.assertEquals(files[4], orderedFiles.get(2));
Assert.assertEquals(files[5], orderedFiles.get(3));
// and then a.xsd
Assert.assertEquals(files[0], orderedFiles.get(4));
// and finally b.xsd
Assert.assertEquals(files[2], orderedFiles.get(5));
}

@Test
public void reorderFilesIncludesWithWildcardMiddle() {
String[] files = {"a.xsd", "common/c1.xsd", "b.xsd", "common/c2.xsd", "common/a.xsd", "common/b.xsd" };
List<String> orderedFiles = IOUtils.reorderFiles(files, new String[] { "a.xsd", "common/*.xsd", "b.xsd" });

Assert.assertNotNull("Ordered files list should not be null",
orderedFiles);
Assert.assertEquals("Ordered files list should contains all elements of initial list",
files.length, orderedFiles.size());

// we have a.xsd
Assert.assertEquals(files[0], orderedFiles.get(0));
// and then all common/*.xsd files in same order
Assert.assertEquals(files[1], orderedFiles.get(1));
Assert.assertEquals(files[3], orderedFiles.get(2));
Assert.assertEquals(files[4], orderedFiles.get(3));
Assert.assertEquals(files[5], orderedFiles.get(4));
// and finally b.xsd
Assert.assertEquals(files[2], orderedFiles.get(5));
}

@Test
public void reorderFilesIncludesWithWildcardLast() {
String[] files = {"a.xsd", "common/c1.xsd", "b.xsd", "common/c2.xsd", "common/a.xsd", "common/b.xsd" };
List<String> orderedFiles = IOUtils.reorderFiles(files, new String[] { "a.xsd", "b.xsd", "common/*.xsd" });

Assert.assertNotNull("Ordered files list should not be null",
orderedFiles);
Assert.assertEquals("Ordered files list should contains all elements of initial list",
files.length, orderedFiles.size());

// we have a.xsd
Assert.assertEquals(files[0], orderedFiles.get(0));
// and then b.xsd
Assert.assertEquals(files[2], orderedFiles.get(1));
// and finally all common/*.xsd files in same order
Assert.assertEquals(files[1], orderedFiles.get(2));
Assert.assertEquals(files[3], orderedFiles.get(3));
Assert.assertEquals(files[4], orderedFiles.get(4));
Assert.assertEquals(files[5], orderedFiles.get(5));
}
}
51 changes: 51 additions & 0 deletions maven-plugin/tests/jt-194/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>jaxb-maven-plugin-tests-194</artifactId>
<parent>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin-tests</artifactId>
<version>2.0.4-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>
<name>JAXB Tools :: Maven Plugin :: Test [JAXB-TOOLS 194]</name>
<description>
This project tests pom artifacts filtering that may cause scanDependenciesForBindings fails on dependency of type pom.
</description>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin-testing</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<scanDependenciesForBindings>true</scanDependenciesForBindings>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 24a1bc4

Please sign in to comment.