Skip to content

Commit

Permalink
Fixing issue JT-194 : adding TypeArtifactFilter to exclude pom artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent SCHOELENS committed Aug 9, 2023
1 parent 93f623a commit 73ec4b2
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -791,13 +791,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
@@ -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
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>
11 changes: 11 additions & 0 deletions maven-plugin/tests/jt-194/src/main/resources/purchaseorder.xjb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jxb:extensionBindingPrefixes="xjc"
jxb:version="2.1">
<jxb:globalBindings generateValueClass="false">
<xjc:simple/>
</jxb:globalBindings>
</jxb:bindings>
66 changes: 66 additions & 0 deletions maven-plugin/tests/jt-194/src/main/resources/purchaseorder.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:annotation>
<xsd:documentation xml:lang="en">
Purchase order schema for Example.com.
Copyright 2000 Example.com. All rights reserved.
</xsd:documentation>
</xsd:annotation>

<xsd:element name="purchaseOrder" type="PurchaseOrderType"/>

<xsd:element name="comment" type="xsd:string"/>

<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:element name="shipTo" type="USAddress"/>
<xsd:element name="billTo" type="USAddress"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="items" type="Items"/>
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>

<xsd:complexType name="USAddress">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN"
fixed="US"/>
</xsd:complexType>

<xsd:complexType name="Items">
<xsd:sequence>
<xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="productName" type="xsd:string"/>
<xsd:element name="quantity">
<xsd:simpleType>
<xsd:restriction base="xsd:positiveInteger">
<xsd:maxExclusive value="100"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="USPrice" type="xsd:decimal"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="partNum" type="SKU" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>

<!-- Stock Keeping Unit, a code for identifying products -->
<xsd:simpleType name="SKU">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{3}-[A-Z]{2}"/>
</xsd:restriction>
</xsd:simpleType>

</xsd:schema>
1 change: 1 addition & 0 deletions maven-plugin/tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<module>gh-issue-58</module>
<module>java-9</module>
<module>jt-250</module>
<module>jt-194</module>
</modules>
<build>
<pluginManagement>
Expand Down

0 comments on commit 73ec4b2

Please sign in to comment.