Skip to content

Commit

Permalink
simplify parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Hervé Boutemy <[email protected]>
  • Loading branch information
hboutemy authored and goneall committed Jul 7, 2023
1 parent 9ba858d commit 14d478c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 44 deletions.
65 changes: 30 additions & 35 deletions src/main/java/org/spdx/maven/CreateSpdxMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.spdx.library.model.license.LicenseInfoFactory;
import org.spdx.library.model.license.SpdxNoAssertionLicense;

import org.spdx.maven.utils.LicenseManager;
import org.spdx.maven.utils.LicenseManagerException;
import org.spdx.maven.utils.LicenseMapperException;
import org.spdx.maven.utils.SpdxBuilderException;
Expand Down Expand Up @@ -80,12 +79,16 @@
* used. If no SPDX standard license is available, a nonStandardLicense must be declared as a parameter including a
* unique license ID and the verbatim license text.
* <p>
* The following SPDX fields are populated from the POM project information: - package name: project name or artifactId
* if the project name is not provided - package description: project description - package shortDescription: project
* description - package downloadUrl: distributionManager url - package homePage: project url - package supplier:
* project organization - package versionInfo: project version - files for analysis: build source files + project
* resource files
* <p>
* The following SPDX fields are populated from the POM project information:<ul>
* <li>package name: project name or artifactId if the project name is not provided</li>
* <li>package description: project description</li>
* <li>package shortDescription: project description</li>
* <li>package downloadUrl: distributionManager url</li>
* <li>package homePage: project url</li>
* <li>package supplier: project organization</li>
* <li>package versionInfo: project version</li>
* <li>files for analysis: build source files + project resource files</li>
* </ul><p>
* Additional SPDX fields are supplied as configuration parameters to this plugin.
*/
@Mojo( name = "createSPDX",
Expand All @@ -105,37 +108,33 @@ public class CreateSpdxMojo extends AbstractMojo

public static final String RDF_OUTPUT_FORMAT = "RDF/XML";

@Parameter( defaultValue = "${project}", readonly = true )
MavenProject mavenProject;
@Component
private MavenProject mavenProject;

@Component
private MavenProjectHelper projectHelper;

@Component
private ProjectBuilder mavenProjectBuilder;

@Parameter( defaultValue = "${session}", readonly = true )
@Component
private MavenSession session;

// Parameters for the plugin
/**
* SPDX File name
*/
@Parameter( defaultValue = "${project.reporting.outputDirectory}/${project.groupId}_${project.artifactId}-${project.version}.spdx",
property = "spdxFileName",
required = true )
property = "spdxFileName" )
private File spdxFile;

/**
* Document namespace - must be unique for the artifact and SPDX file
*/
@Parameter( defaultValue = "http://spdx.org/spdxpackages/${project.groupId}_${project.artifactId}-${project.version}",
property = "spdxDocumentNamespace",
required = true )
@Parameter( defaultValue = "http://spdx.org/spdxpackages/${project.groupId}_${project.artifactId}-${project.version}" )
private String spdxDocumentNamespace;

@Parameter( defaultValue = "${project.basedir}",
property = "componentName" )
@Parameter( defaultValue = "${project.basedir}" )
private String componentName;

/**
Expand Down Expand Up @@ -338,18 +337,18 @@ public class CreateSpdxMojo extends AbstractMojo
* Note: in this case the non-specified SPDX fields for the lowest level PathSpecificSpdxInfo will use the default
* project level fields NOT the higher level PathSpecificSpdxInfo.
*/
@Parameter( required = false )
@Parameter
private PathSpecificSpdxInfo[] pathsWithSpecificSpdxInfo;

@Parameter( required = false )
@Parameter
private ExternalReference[] externalReferences;

/**
* Output file format for the SPDX file. One of:
* - JSON - JSON SPDX format
* - RDF/XML - RDF/XML format
*/
@Parameter( required = false )
@Parameter( defaultValue = "JSON" )
private String outputFormat;

/**
Expand All @@ -367,7 +366,6 @@ public class CreateSpdxMojo extends AbstractMojo
@Parameter( defaultValue = "true" )
private boolean includeTransitiveDependencies;

@SuppressWarnings( "deprecation" )
public void execute() throws MojoExecutionException
{
OutputFormat outputFormatEnum = prepareOutput();
Expand All @@ -384,7 +382,7 @@ public void execute() throws MojoExecutionException
SpdxProjectInformation projectInformation;
try
{
projectInformation = getSpdxProjectInfoFromParameters( builder.getLicenseManager(), spdxDoc );
projectInformation = getSpdxProjectInfoFromParameters( builder );
}
catch ( InvalidSPDXAnalysisException e2 )
{
Expand All @@ -394,6 +392,7 @@ public void execute() throws MojoExecutionException
SpdxDefaultFileInformation defaultFileInformation = getDefaultFileInfoFromParameters( spdxDoc );
HashMap<String, SpdxDefaultFileInformation> pathSpecificInformation = getPathSpecificInfoFromParameters( defaultFileInformation, spdxDoc );

@SuppressWarnings("deprecation")
Set<Artifact> dependencies = includeTransitiveDependencies ? mavenProject.getArtifacts() : mavenProject.getDependencyArtifacts();

if ( getLog().isDebugEnabled() )
Expand All @@ -409,7 +408,7 @@ public void execute() throws MojoExecutionException
SpdxDependencyInformation dependencyInformation = null;
try
{
dependencyInformation = getSpdxDependencyInformation( dependencies, builder.getLicenseManager(), spdxDoc );
dependencyInformation = getSpdxDependencyInformation( dependencies, builder );
}
catch ( LicenseMapperException e1 )
{
Expand All @@ -423,8 +422,7 @@ public void execute() throws MojoExecutionException
try
{
builder.buildDocumentFromFiles( sources, mavenProject.getBasedir().getAbsolutePath(), projectInformation,
defaultFileInformation, pathSpecificInformation, dependencyInformation, getChecksumAlgorithms(),
spdxDoc.getDocumentUri() );
defaultFileInformation, pathSpecificInformation, dependencyInformation, getChecksumAlgorithms() );
}
catch ( SpdxBuilderException e )
{
Expand Down Expand Up @@ -522,17 +520,15 @@ private SpdxDocumentBuilder initSpdxDocumentBuilder( OutputFormat outputFormatEn
* Collect dependency information from Maven dependencies
*
* @param dependencies Maven dependencies
* @param licenseManager
* @param spdxDoc SPDX document to contain the dependencies
* @param builder SPDX document builder
* @return information collected from Maven dependencies
* @throws LicenseMapperException
* @throws InvalidSPDXAnalysisException
*/
private SpdxDependencyInformation getSpdxDependencyInformation( Set<Artifact> dependencies,
LicenseManager licenseManager,
SpdxDocument spdxDoc ) throws LicenseMapperException, InvalidSPDXAnalysisException
SpdxDocumentBuilder builder ) throws LicenseMapperException, InvalidSPDXAnalysisException
{
SpdxDependencyInformation retval = new SpdxDependencyInformation( licenseManager, spdxDoc, createExternalRefs );
SpdxDependencyInformation retval = new SpdxDependencyInformation( builder.getLicenseManager(), builder.getSpdxDoc(), createExternalRefs );
if ( dependencies != null )
{
for ( Artifact dependency : dependencies )
Expand Down Expand Up @@ -735,14 +731,13 @@ private SpdxDefaultFileInformation getDefaultFileInfoFromParameters( SpdxDocumen
* description and SPDX package summary supplier - the project organization is used for the supplier. "ORGANIZATION:
* " is prepended
*
* @param licenseManager maps the Maven licenses to SPDX licenses
* @param spdxDoc SPDX document containing any extracted license infos
* @param builder SPDX document builder
* @return
* @throws MojoExecutionException
*/
private SpdxProjectInformation getSpdxProjectInfoFromParameters( LicenseManager licenseManager,
SpdxDocument spdxDoc ) throws MojoExecutionException, InvalidSPDXAnalysisException
private SpdxProjectInformation getSpdxProjectInfoFromParameters( SpdxDocumentBuilder builder ) throws MojoExecutionException, InvalidSPDXAnalysisException
{
SpdxDocument spdxDoc = builder.getSpdxDoc();
SpdxProjectInformation retval = new SpdxProjectInformation();
if ( this.documentComment != null )
{
Expand All @@ -754,7 +749,7 @@ private SpdxProjectInformation getSpdxProjectInfoFromParameters( LicenseManager
List<License> mavenLicenses = mavenProject.getLicenses();
try
{
declaredLicense = licenseManager.mavenLicenseListToSpdxLicense( mavenLicenses );
declaredLicense = builder.getLicenseManager().mavenLicenseListToSpdxLicense( mavenLicenses );
}
catch ( LicenseManagerException e )
{
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/org/spdx/maven/utils/SpdxDocumentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.spdx.library.SpdxConstants;
import org.spdx.library.SpdxVerificationHelper;
import org.spdx.library.model.Annotation;
import org.spdx.library.model.ExternalRef;
import org.spdx.library.model.Relationship;
import org.spdx.library.model.SpdxCreatorInformation;
import org.spdx.library.model.SpdxDocument;
Expand Down Expand Up @@ -202,8 +201,7 @@ public void buildDocumentFromFiles( List<FileSet> sources,
SpdxDefaultFileInformation defaultFileInformation,
Map<String, SpdxDefaultFileInformation> pathSpecificInformation,
SpdxDependencyInformation dependencyInformation,
Set<ChecksumAlgorithm> algorithms,
String spdxDocumentNamespace ) throws SpdxBuilderException
Set<ChecksumAlgorithm> algorithms ) throws SpdxBuilderException
{
try (FileOutputStream spdxOut = new FileOutputStream( spdxFile ))
{
Expand All @@ -212,7 +210,7 @@ public void buildDocumentFromFiles( List<FileSet> sources,
collectSpdxFileInformation( sources,
baseDir, defaultFileInformation, spdxFile.getPath().replace( "\\", "/" ), pathSpecificInformation, algorithms );
addDependencyInformation( dependencyInformation );
modelStore.serialize( spdxDocumentNamespace, spdxOut );
modelStore.serialize( spdxDoc.getDocumentUri(), spdxOut );
LOG.debug( "Completed build document from files" );
}
catch ( FileNotFoundException e )
Expand Down Expand Up @@ -314,9 +312,9 @@ private void fillSpdxDocumentInformation( SpdxProjectInformation projectInformat
private Collection<Annotation> toSpdxAnnotations( org.spdx.maven.Annotation[] annotations ) throws MojoExecutionException
{
List<Annotation> retval = new ArrayList<>();
for ( int i = 0; i < annotations.length; i++ )
for ( org.spdx.maven.Annotation annotation: annotations )
{
retval.add( annotations[i].toSpdxAnnotation( spdxDoc ) );
retval.add( annotation.toSpdxAnnotation( spdxDoc ) );
}
return retval;
}
Expand Down Expand Up @@ -383,7 +381,7 @@ private SpdxPackage createSpdxPackage( SpdxProjectInformation projectInformation
}
try
{
// description
// description
if ( projectInformation.getDescription() != null )
{
pkg.setDescription( projectInformation.getDescription() );
Expand Down Expand Up @@ -464,8 +462,7 @@ private SpdxPackage createSpdxPackage( SpdxProjectInformation projectInformation
ExternalReference[] externalRefs = projectInformation.getExternalRefs();
if ( externalRefs != null && externalRefs.length > 0 )
{
ExternalRef[] externalRefAr = new ExternalRef[externalRefs.length];
for ( ExternalReference externalRef: externalRefs )
for ( ExternalReference externalRef : externalRefs )
{
try
{
Expand Down

0 comments on commit 14d478c

Please sign in to comment.