Skip to content
This repository has been archived by the owner on Dec 20, 2022. It is now read-only.

Enabling Maven scan mode #118

Merged
merged 2 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ExecutionParameters {
private ArchiveType archiveType;

/**
* Directory to scan for archives.
* Location to scan for archives.
*/
private ScanLocation scanLocation;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void execute() throws LicenseScoutExecutionException, LicenseScoutFailOnE
final AbstractFinder finder = FinderFactory.getInstance().createFinder(executionParameters, licenseStoreData,
finderParameters);
getLog().info("Starting scan on "
+ getExecutionParameters().getScanLocation().getScanDirectory().getAbsolutePath() + "...");
+ getExecutionParameters().getScanLocation().toLogString() + "...");

OutputResult outputResult;
try {
Expand Down Expand Up @@ -441,15 +441,26 @@ private OutputResult createOutputResult(final FinderResult finderResult) {
}

private void checkParameters(final ILSLog log) throws LicenseScoutExecutionException {
final File scanDirectory = getExecutionParameters().getScanLocation().getScanDirectory();
if (scanDirectory != null) {
if (!scanDirectory.exists()) {
throw new LicenseScoutExecutionException(
"This scan directory does not exist: " + scanDirectory.getAbsolutePath());
final ScanLocation scanLocation = getExecutionParameters().getScanLocation();
if (scanLocation != null) {
List<File> scanFiles = scanLocation.getScanFiles();
if (scanFiles != null && !scanFiles.isEmpty()) {
// OK
log.info("using scan files: " + scanFiles);
} else {
final File scanDirectory = scanLocation.getScanDirectory();
if (scanDirectory != null) {
if (!scanDirectory.exists()) {
throw new LicenseScoutExecutionException(
"This scan directory does not exist: " + scanDirectory.getAbsolutePath());
}
log.info("using scan directory: " + scanDirectory.getAbsolutePath());
} else {
throw new LicenseScoutExecutionException("neither scanFiles nor scanDirectory configured");
}
}
log.info("using scan directory: " + scanDirectory.getAbsolutePath());
} else {
throw new LicenseScoutExecutionException("scanDirectory not configured");
throw new LicenseScoutExecutionException("scan location not configured");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ private VelocityContext createVelocityContext(final OutputResult outputResult,
final List<Archive> archiveFiles = getSortedArchives(outputResult);
context.put("archiveFiles", archiveFiles);

context.put("sourcePath", outputResult.getFinderResult().getScanDirectory().getAbsolutePath());
final File scanDirectory = outputResult.getFinderResult().getScanDirectory();
context.put("sourcePath", scanDirectory != null ? scanDirectory.getAbsolutePath() : "");
context.put("detectionStatusStatistics", outputResult.getDetectionStatusStatistics());
context.put("legalStatusStatistics", outputResult.getLegalStatusStatistics());
context.put("generalStatistics", outputResult.getGeneralStatistics());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ protected final ILSLog getLog() {
}

/**
* Sets the directory to scan.
* Sets the scan location.
*
* @param scanLocation the directory to scan
* @param scanLocation the location to scan
*/
public final void setScanLocation(final ScanLocation scanLocation) {
this.scanLocation = scanLocation;
Expand All @@ -97,6 +97,9 @@ public final void setScanLocation(final ScanLocation scanLocation) {
/**
* Obtains the scan location.
*
* <p>Is intended to be used for logging. For processing, {@link #getScanDirectory()}
* and {@link #getScanFiles()} should be used.</p>
*
* @return the scan location
*/
protected final ScanLocation getScanLocation() {
Expand All @@ -114,6 +117,17 @@ protected final File getScanDirectory() {
return getScanLocation().getScanDirectory();
}

/**
* Obtains the files to scan.
*
* @return the scanDirectory
*
* @see #getScanDirectory()
*/
protected final List<File> getScanFiles() {
return scanLocation.getScanFiles();
}

/**
* Adds an archive to the list of found archives.
* @param foundArchive an archive
Expand All @@ -133,7 +147,7 @@ public FinderResult findLicenses() throws Exception {
if (debug) {
printArchiveList(archiveFiles);
}
getLog().info("Finished scanning for licenses in " + getScanDirectory().getAbsolutePath());
getLog().info("Finished scanning for licenses in " + getScanLocation().toLogString());
return new FinderResult(getScanLocation(), archiveFiles);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;

import org.aposin.licensescout.archive.Archive;
import org.aposin.licensescout.execution.ScanLocation;
import org.aposin.licensescout.license.IArtifactServerUtil;
import org.aposin.licensescout.license.LicenseStoreData;
import org.aposin.licensescout.util.ArchiveMetaInformation;
Expand All @@ -36,20 +37,8 @@
* a starting point.</p>
*
*/
// TODO: rename to JavaDirectoryFinder
public class JavaJarFinder extends AbstractJavaFinder {

protected enum ScanMode {
/**
* In a directory in the file system that is not an archive.
*/
DIRECTORY(),
/**
* In a directory in the file system that is an archive (i.e. in an unpacked archive).
*/
UNPACKED_ARCHIVE();
}

private final List<String> specialArchiveNames = new ArrayList<>();
private final FinderHandler<File, FileSystemEntryContainer, File> fileSystemFinderHandler;

Expand Down Expand Up @@ -77,9 +66,17 @@ private void initSpecialArchiveNames() {
*/
@Override
protected void findLicensesImpl() throws Exception {
final File root = getScanDirectory();
final String filePath = "";
parseFile(root, filePath);
final File rootDirectory = getScanDirectory();
if (rootDirectory != null) {
final String filePath = "";
parseFile(rootDirectory, filePath);
} else {
final List<File> rootFiles = getScanFiles();
for (final File rootFile : rootFiles) {
final String filePath = "";
parseFile(rootFile, filePath);
}
}
}

/**
Expand Down Expand Up @@ -200,9 +197,6 @@ private Archive createAndAddArchive(final String fileName, final ArchiveMetaInfo
* @return true, if is archive
*/
private static boolean isArchiveDirectory(final File dir) {
if (!dir.isDirectory()) {
return false;
}
final File metaInfEntry = findEntry(dir, "META-INF");
if (metaInfEntry == null || !metaInfEntry.isDirectory()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,29 @@ public void testExecutionJavaNoOutputs() throws Exception {
* @throws Exception
*/
@Test
public void testExecutionJavaWithOutputs() throws Exception {
public void testExecutionJavaWithOutputsScanDirectory() throws Exception {
final File scanDirectory = new File("src/test/resources/scans/empty");
final ArrayList<ExecutionOutput> outputs = createTripleOutput();
final ExecutionParameters executionParameters = createExecutionParameters(ArchiveType.JAVA, scanDirectory,
outputs);
assertExecution(executionParameters);
}

/**
* Test case for the method {@link Executor#execute()}.
*
* @throws Exception
*/
@Test
public void testExecutionJavaWithOutputsScanFiles() throws Exception {
final File scanDirectory = new File("src/test/resources/scans/empty");
final ScanLocation scanLocation = new ScanLocation(Arrays.asList(scanDirectory));
final ArrayList<ExecutionOutput> outputs = createTripleOutput();
final ExecutionParameters executionParameters = createExecutionParameters(ArchiveType.JAVA, scanLocation,
outputs, true);
assertExecution(executionParameters);
}

/**
* Test case for the method {@link Executor#execute()}.
*
Expand Down Expand Up @@ -211,8 +226,9 @@ public void testExecutionJavaWriteToDatabaseNoDatabaseConfiguredSnapshotVersionW
@Test(expected = LicenseScoutExecutionException.class)
public void testExecutionJavaNoReportExporterFactory() throws Exception {
final File scanDirectory = new File("src/test/resources/scans/java-unpacked-license-manifest");
final ScanLocation scanLocation = new ScanLocation(scanDirectory);
final ArrayList<ExecutionOutput> outputs = createTripleOutput();
final ExecutionParameters executionParameters = createExecutionParameters(ArchiveType.JAVA, scanDirectory,
final ExecutionParameters executionParameters = createExecutionParameters(ArchiveType.JAVA, scanLocation,
outputs, false);
assertExecution(executionParameters);
}
Expand Down Expand Up @@ -243,6 +259,18 @@ public void testExecutionJavaNotExistingScanDirectory() throws Exception {
assertExecution(executionParameters);
}

/**
* Test case for the method {@link Executor#execute()}.
*
* @throws Exception
*/
@Test(expected = LicenseScoutExecutionException.class)
public void testExecutionJavaNoScanLocation() throws Exception {
final ExecutionParameters executionParameters = createExecutionParametersNoOutputs(ArchiveType.JAVA,
(ScanLocation) null);
assertExecution(executionParameters);
}

/**
* Test case for the method {@link Executor#execute()}.
*
Expand Down Expand Up @@ -311,25 +339,33 @@ private ExecutionParameters createExecutionParametersNoOutputs(final ArchiveType
return createExecutionParameters(archiveType, scanDirectory, outputs);
}

private ExecutionParameters createExecutionParametersNoOutputs(final ArchiveType archiveType,
final ScanLocation scanLocation) {
final ArrayList<ExecutionOutput> outputs = new ArrayList<>();
return createExecutionParameters(archiveType, scanLocation, outputs, true);
}

private ExecutionParameters createExecutionParameters(final ArchiveType archiveType, final File scanDirectory,
final ArrayList<ExecutionOutput> outputs) {
return createExecutionParameters(archiveType, scanDirectory, outputs, true);
final ScanLocation scanLocation = new ScanLocation(scanDirectory);
return createExecutionParameters(archiveType, scanLocation, outputs, true);
}

/**
* @param archiveType
* @param scanDirectory
* @param scanLocation the scan location
* @param outputs
* @param withStandardReportExporterFactory
* @return an execution parameters instance
* @return
*/
private ExecutionParameters createExecutionParameters(final ArchiveType archiveType, final File scanDirectory,
private ExecutionParameters createExecutionParameters(final ArchiveType archiveType,
final ScanLocation scanLocation,
final ArrayList<ExecutionOutput> outputs,
final boolean withStandardReportExporterFactory) {
final ExecutionParameters executionParameters = new ExecutionParameters();
executionParameters.setArchiveType(archiveType);
executionParameters.setFilteredVendorNames(new ArrayList<>());
executionParameters.setScanLocation(new ScanLocation(scanDirectory));
executionParameters.setScanLocation(scanLocation);
executionParameters.setOutputDirectory(new File("target"));
executionParameters.setOutputs(outputs);
executionParameters.setCleanOutputActive(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ In one execution, LicenseScout can either scan for Java artifacts or for Javascr

=== Scan Location

The base directory where archives are searched for (recursively and also inside JARs) is configured by the parameter `scanDirectory`.
The location to be scanned can either be:

* a base directory where archives are searched for (recursively and also inside JARs). This is configured by the parameter `scanDirectory`.
* a list of Maven artifacts. The configured artifacts and their (transitive) dependencies are scanned. This is configured by the parameter `scanArtifacts`.
The parameter `scanArtifactScope` optionally allows to specify the
scope for dependency resolution.

[[output-types-and-files]]
=== Output types and files
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.aposin.licensescout.test</groupId>
<artifactId>org.aposin.licensescout.test.licensescout</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Simple Test for License Scout Maven Plugin</name>

<url>https://aposin.org/</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<scanArtifacts>
<scanArtifact>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
</scanArtifact>
</scanArtifacts>
<outputs>
<output>
<type>CSV</type>
</output>
</outputs>
<configurationBundle>
<groupId>@project.groupId@</groupId>
<artifactId>org.aposin.licensescout.configuration.sample</artifactId>
<version>@project.version@</version>
<classifier>configuration</classifier>
<type>zip</type>
</configurationBundle>
</configuration>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>scanJava</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright 2019 Association for the promotion of open-source insurance software and for the establishment of open interface standards in the insurance industry (Verein zur Förderung quelloffener Versicherungssoftware und Etablierung offener Schnittstellenstandards in der Versicherungsbranche)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
File reportFile = new File( basedir, "target/licensescout/licensereport.csv" );
assert reportFile.isFile();
Loading