Skip to content

Commit

Permalink
Default value of generatePurls to true and urlencode artifactId
Browse files Browse the repository at this point in the history
  • Loading branch information
rogelio-o committed Jan 19, 2024
1 parent bc42d82 commit c5ae407
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/spdx/maven/CreateSpdxMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ public class CreateSpdxMojo extends AbstractMojo
* and locator "pkg:maven/${project.groupId}/${project.artifactId}@${project.version}".
*/
@Parameter( property = "spdx.generatePurls" )
private boolean generatePurls;
private boolean generatePurls = true;

public void execute() throws MojoExecutionException
{
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/spdx/maven/utils/SpdxExternalRefBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.spdx.maven.utils;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import org.apache.maven.artifact.Artifact;
Expand All @@ -26,9 +28,8 @@ private static ExternalRef generatePurlExternalRef( SpdxDocument spdxDoc, MavenP
{
try
{
String purl = "pkg:maven/" + project.getGroupId() + "/" + project.getArtifactId() + "@" + project.getVersion();
return spdxDoc.createExternalRef( ReferenceCategory.PACKAGE_MANAGER, new ReferenceType("http://spdx.org/rdf/references/purl"),
purl, null );
generatePurl( project ), null );
}
catch ( InvalidSPDXAnalysisException e )
{
Expand All @@ -37,4 +38,11 @@ private static ExternalRef generatePurlExternalRef( SpdxDocument spdxDoc, MavenP
}
}

private static String generatePurl( MavenProject project )
{
return "pkg:maven/" + project.getGroupId() + "/"
+ URLEncoder.encode( project.getArtifactId(), StandardCharsets.UTF_8 )
+ "@" + project.getVersion();
}

}
19 changes: 9 additions & 10 deletions src/test/java/org/spdx/maven/TestSpdxMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.spdx.jacksonstore.MultiFormatStore;
import org.spdx.jacksonstore.MultiFormatStore.Format;
import org.spdx.library.ModelCopyManager;
import org.spdx.library.model.ExternalRef;
import org.spdx.library.model.ReferenceType;
import org.spdx.library.model.SpdxDocument;
import org.spdx.library.model.SpdxElement;
import org.spdx.library.model.SpdxFile;
Expand Down Expand Up @@ -79,7 +77,7 @@ public void testExecute() throws Exception
assertTrue( spdxFile.exists() );
// Test output artifact file is created
File artifactFile = getTestFile(
"target/test-classes/unit/spdx-maven-plugin-test/spdx-maven-plugin-test.spdx.rdf.xml" );
"target/test-classes/unit/spdx-maven-plugin-test/spdx maven plugin test.spdx.rdf.xml" );
assertTrue( artifactFile.exists() );
ISerializableModelStore modelStore = new RdfStore();
ModelCopyManager copyManager = new ModelCopyManager();
Expand Down Expand Up @@ -323,7 +321,7 @@ public void testExecuteUseArtfactId() throws Exception
assertTrue( spdxFile.exists() );
// Test output artifact file is created
File artifactFile = getTestFile(
"target/test-classes/unit/spdx-maven-plugin-test/spdx-maven-plugin-test.spdx.json" );
"target/test-classes/unit/spdx-maven-plugin-test/spdx maven plugin test.spdx.json" );
assertTrue( artifactFile.exists() );
ISerializableModelStore modelStore = new MultiFormatStore( new InMemSpdxStore(), Format.JSON );
ModelCopyManager copyManager = new ModelCopyManager();
Expand Down Expand Up @@ -433,7 +431,7 @@ else if ( creator.equals( "Person: Creator2" ) )
assertTrue( described instanceof SpdxPackage );
SpdxPackage pkg = (SpdxPackage) described;
// name
assertEquals( "org.spdx:spdx-maven-plugin-test", pkg.getName().get() );
assertEquals( "org.spdx:spdx maven plugin test", pkg.getName().get() );
// packageAnnotations
assertEquals( 1, pkg.getAnnotations().size() );
org.spdx.library.model.Annotation annotation = pkg.getAnnotations().toArray( new org.spdx.library.model.Annotation [pkg.getAnnotations().size()] )[0];
Expand Down Expand Up @@ -569,7 +567,7 @@ public void testExecuteJson() throws Exception
assertTrue( spdxFile.exists() );
// Test output artifact file is created
File artifactFile = getTestFile(
"target/test-classes/unit/spdx-maven-plugin-test/spdx-maven-plugin-test.spdx.json" );
"target/test-classes/unit/spdx-maven-plugin-test/spdx maven plugin test.spdx.json" );
assertTrue( artifactFile.exists() );
ISerializableModelStore modelStore = new MultiFormatStore( new InMemSpdxStore(), Format.JSON );
ModelCopyManager copyManager = new ModelCopyManager();
Expand Down Expand Up @@ -852,7 +850,7 @@ public void testExecuteUriNotUrl() throws Exception
assertTrue( spdxFile.exists() );
// Test output artifact file is created
File artifactFile = getTestFile(
"target/test-classes/unit/spdx-maven-plugin-test/spdx-maven-plugin-test.spdx.rdf.xml" );
"target/test-classes/unit/spdx-maven-plugin-test/spdx maven plugin test.spdx.rdf.xml" );
assertTrue( artifactFile.exists() );
ISerializableModelStore modelStore = new RdfStore();
ModelCopyManager copyManager = new ModelCopyManager();
Expand Down Expand Up @@ -885,7 +883,7 @@ public void testExecuteNoContributors() throws Exception
assertTrue( spdxFile.exists() );
// Test output artifact file is created
File artifactFile = getTestFile(
"target/test-classes/unit/spdx-maven-plugin-test/spdx-maven-plugin-test.spdx.rdf.xml" );
"target/test-classes/unit/spdx-maven-plugin-test/spdx maven plugin test.spdx.rdf.xml" );
assertTrue( artifactFile.exists() );
ISerializableModelStore modelStore = new RdfStore();
ModelCopyManager copyManager = new ModelCopyManager();
Expand Down Expand Up @@ -1127,7 +1125,7 @@ public void testExecuteUseGeneratePurls() throws Exception
assertTrue( spdxFile.exists() );
// Test output artifact file is created
File artifactFile = getTestFile(
"target/test-classes/unit/spdx-maven-plugin-test/spdx-maven-plugin-test.spdx.json" );
"target/test-classes/unit/spdx-maven-plugin-test/spdx maven plugin test.spdx.json" );
assertTrue( artifactFile.exists() );
ISerializableModelStore modelStore = new MultiFormatStore( new InMemSpdxStore(), Format.JSON );
ModelCopyManager copyManager = new ModelCopyManager();
Expand Down Expand Up @@ -1156,7 +1154,8 @@ public void testExecuteUseGeneratePurls() throws Exception
assertEquals( externalRef.getReferenceCategory(), ReferenceCategory.PACKAGE_MANAGER );
assertEquals( externalRef.getReferenceType().getIndividualURI(), "http://spdx.org/rdf/references/purl");
assertEquals( externalRef.getReferenceLocator(),
"pkg:maven/" + pkg.getName().get().replace(":", "/") + "@" + pkg.getVersionInfo().get() );
"pkg:maven/" + pkg.getName().get().replace( ":", "/" ).replaceAll( " ", "+" )
+ "@" + pkg.getVersionInfo().get() );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>org.spdx</groupId>
<artifactId>spdx-maven-plugin-test</artifactId>
<artifactId>spdx maven plugin test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test SPDX Plugin</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>org.spdx</groupId>
<artifactId>spdx-maven-plugin-test</artifactId>
<artifactId>spdx maven plugin test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test SPDX Plugin</name>
Expand Down Expand Up @@ -231,6 +231,7 @@
</snippets>
</PathSpecificSpdxInfo>
</pathsWithSpecificSpdxInfo>
<generatePurls>false</generatePurls>
</configuration>
</plugin>
</plugins>
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/unit/spdx-maven-plugin-test/json-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>org.spdx</groupId>
<artifactId>spdx-maven-plugin-test</artifactId>
<artifactId>spdx maven plugin test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test SPDX Plugin</name>
Expand Down Expand Up @@ -230,6 +230,7 @@
</snippets>
</PathSpecificSpdxInfo>
</pathsWithSpecificSpdxInfo>
<generatePurls>false</generatePurls>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>org.spdx</groupId>
<artifactId>spdx-maven-plugin-test</artifactId>
<artifactId>spdx maven plugin test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test SPDX Plugin</name>
Expand Down Expand Up @@ -231,6 +231,7 @@
</snippets>
</PathSpecificSpdxInfo>
</pathsWithSpecificSpdxInfo>
<generatePurls>false</generatePurls>
</configuration>
</plugin>
</plugins>
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/unit/spdx-maven-plugin-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>org.spdx</groupId>
<artifactId>spdx-maven-plugin-test</artifactId>
<artifactId>spdx maven plugin test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test SPDX Plugin</name>
Expand Down Expand Up @@ -235,6 +235,7 @@
</snippets>
</PathSpecificSpdxInfo>
</pathsWithSpecificSpdxInfo>
<generatePurls>false</generatePurls>
</configuration>
</plugin>
</plugins>
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/unit/spdx-maven-plugin-test/uri-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>org.spdx</groupId>
<artifactId>spdx-maven-plugin-test</artifactId>
<artifactId>spdx maven plugin test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test SPDX Plugin</name>
Expand Down Expand Up @@ -235,6 +235,7 @@
</snippets>
</PathSpecificSpdxInfo>
</pathsWithSpecificSpdxInfo>
<generatePurls>false</generatePurls>
</configuration>
</plugin>
</plugins>
Expand Down

0 comments on commit c5ae407

Please sign in to comment.