Skip to content

Commit

Permalink
Issue 145: Fix aggregate-add-third-party modifying project dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
srdo committed Oct 4, 2018
1 parent abeab62 commit 2308f69
Show file tree
Hide file tree
Showing 19 changed files with 303 additions and 56 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<scriptVariables>
<projectVersion>${project.version}</projectVersion>
</scriptVariables>
</configuration>
</plugin>
</plugins>
</pluginManagement>
Expand Down
23 changes: 23 additions & 0 deletions src/it/ISSUE-145/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
###
# #%L
# License Maven Plugin
# %%
# Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
# %%
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Lesser Public License for more details.
#
# You should have received a copy of the GNU General Lesser Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/lgpl-3.0.html>.
# #L%
###
invoker.goals=clean package
invoker.failureBehavior=fail-fast
59 changes: 59 additions & 0 deletions src/it/ISSUE-145/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<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.codehaus.mojo.license.test</groupId>
<artifactId>test-ISSUE-145</artifactId>
<version>@pom.version@</version>
<name>License Test :: ISSUE-145</name>
<packaging>pom</packaging>

<licenses>
<license>
<name>The GNU Lesser General Public License, Version 3.0</name>
<url>http://www.gnu.org/licenses/lgpl-3.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<license.verbose>true</license.verbose>
<license.sortByGroupIdAndArtifactId>true</license.sortByGroupIdAndArtifactId>
<licensesOutputFile>${project.build.directory}/licenses.xml</licensesOutputFile>
</properties>

<modules>
<module>submodule1</module>
<module>submodule2</module>
</modules>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
</plugin>
</plugins>

<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>@pom.version@</version>
<executions>
<execution>
<id>aggregate-add-third-party</id>
<goals>
<goal>aggregate-add-third-party</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
27 changes: 27 additions & 0 deletions src/it/ISSUE-145/postbuild.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* #%L
* License Maven Plugin
* %%
* Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/
import java.util.jar.JarFile

file = new JarFile(new File(basedir, 'submodule1/target/test-ISSUE-145-submodule1-' + projectVersion + '.jar'));
expectedLibPath = 'org/codehaus/mojo/license/AnImportantDependency.class';
assert file.getJarEntry(expectedLibPath) != null;
return true;
49 changes: 49 additions & 0 deletions src/it/ISSUE-145/submodule1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<groupId>org.codehaus.mojo.license.test</groupId>
<artifactId>test-ISSUE-145</artifactId>
<version>@pom.version@</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>

<artifactId>test-ISSUE-145-submodule1</artifactId>

<dependencies>
<dependency>
<groupId>org.codehaus.mojo.license.test</groupId>
<artifactId>test-ISSUE-145-submodule2</artifactId>
<version>@pom.version@</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.codehaus.mojo.license;

public class Application {

public static void main(String[] args) {
}
}
13 changes: 13 additions & 0 deletions src/it/ISSUE-145/submodule2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<groupId>org.codehaus.mojo.license.test</groupId>
<artifactId>test-ISSUE-145</artifactId>
<version>@pom.version@</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>test-ISSUE-145-submodule2</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

package org.codehaus.mojo.license;

public class AnImportantDependency {

}
2 changes: 1 addition & 1 deletion src/it/ISSUE-80/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<artifactId>test-ISSUE-59</artifactId>
<version>@pom.version@</version>

<name>License Test :: ISSUE-59</name>
<name>License Test :: ISSUE-80</name>
<packaging>jar</packaging>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import org.apache.maven.model.Dependency;
import org.codehaus.mojo.license.api.LoadedProjectDependencies;

/*
* #%L
Expand Down Expand Up @@ -559,6 +561,12 @@ public abstract class AbstractAddThirdPartyMojo
* @since 1.4
*/
private Map<String, String> globalKnownLicenses;

boolean isAggregatorBuild;

Map<String, List<Dependency>> reactorProjectDependencies;

LoadedProjectDependencies dependencyArtifacts;

// ----------------------------------------------------------------------
// Abstract Methods
Expand Down Expand Up @@ -634,6 +642,12 @@ protected void init()
// not generating bundled file
doGenerateBundle = false;
}

if (isAggregatorBuild) {
dependencyArtifacts = dependenciesTool.loadProjectArtifacts( localRepository, remoteRepositories, project, reactorProjectDependencies );
} else {
dependencyArtifacts = new LoadedProjectDependencies(project.getArtifacts(), project.getDependencyArtifacts());
}

projectDependencies = loadDependencies();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import org.codehaus.mojo.license.api.LoadedProjectDependencies;

/**
* Created on 23/05/16.
Expand Down Expand Up @@ -263,7 +264,9 @@ protected MavenProject getProject()

protected SortedMap<String, MavenProject> getDependencies( MavenProject project )
{
return dependenciesTool.loadProjectDependencies( project, this, localRepository, remoteRepositories, null );
return dependenciesTool.loadProjectDependencies(
new LoadedProjectDependencies(project.getArtifacts(), project.getDependencyArtifacts()),
this, localRepository, remoteRepositories, null );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeSet;
import org.apache.maven.artifact.Artifact;
import org.codehaus.mojo.license.api.LoadedProjectDependencies;

/**
* Base class for third-party reports.
Expand Down Expand Up @@ -516,16 +518,19 @@ Collection<ThirdPartyDetails> createThirdPartyDetails( MavenProject project, boo
DependenciesToolException, MojoExecutionException
{

if ( loadArtifacts )
{
dependenciesTool.loadProjectArtifacts( localRepository, project.getRemoteArtifactRepositories(), project , null);
LoadedProjectDependencies loadedDependencies;
if ( loadArtifacts ) {
loadedDependencies = dependenciesTool.loadProjectArtifacts( localRepository, licenseMerges, project, null );
}

else {
loadedDependencies = new LoadedProjectDependencies(getProject().getArtifacts(), getProject().getDependencyArtifacts());
}

ThirdPartyHelper thirdPartyHelper =
new DefaultThirdPartyHelper( project, encoding, verbose, dependenciesTool, thirdPartyTool, localRepository,
project.getRemoteArtifactRepositories(), getLog() );
// load dependencies of the project
SortedMap<String, MavenProject> projectDependencies = thirdPartyHelper.loadDependencies( this );
SortedMap<String, MavenProject> projectDependencies = thirdPartyHelper.loadDependencies( this, loadedDependencies );

// create licenseMap from it
LicenseMap licenseMap = thirdPartyHelper.createLicenseMap( projectDependencies );
Expand All @@ -547,7 +552,7 @@ Collection<ThirdPartyDetails> createThirdPartyDetails( MavenProject project, boo
{
// Resolve unsafe dependencies using missing files, this will update licenseMap and unsafeDependencies
thirdPartyHelper.createUnsafeMapping( licenseMap, missingFile, missingFileUrl, useRepositoryMissingFiles,
dependenciesWithNoLicense, projectDependencies );
dependenciesWithNoLicense, projectDependencies, loadedDependencies.getAllDependencies() );
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/main/java/org/codehaus/mojo/license/AddThirdPartyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import org.apache.commons.collections.CollectionUtils;
Expand All @@ -21,6 +22,7 @@
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingException;
import org.codehaus.mojo.license.api.LoadedProjectDependencies;
import org.codehaus.mojo.license.api.MavenProjectDependenciesConfigurator;
import org.codehaus.mojo.license.api.ThirdPartyToolException;
import org.codehaus.mojo.license.model.LicenseMap;
Expand Down Expand Up @@ -192,7 +194,7 @@ protected void doAction()
@Override
protected SortedMap<String, MavenProject> loadDependencies()
{
return getHelper().loadDependencies( this );
return getHelper().loadDependencies( this, dependencyArtifacts );
}

/**
Expand All @@ -202,13 +204,13 @@ protected SortedMap<String, MavenProject> loadDependencies()
protected SortedProperties createUnsafeMapping()
throws ProjectBuildingException, IOException, ThirdPartyToolException, MojoExecutionException
{

SortedSet<MavenProject> unsafeDependencies = getUnsafeDependencies();

SortedProperties unsafeMappings =
getHelper().createUnsafeMapping( getLicenseMap(), getMissingFile(), missingFileUrl,
useRepositoryMissingFiles, unsafeDependencies,
getProjectDependencies() );
getProjectDependencies(), dependencyArtifacts.getAllDependencies() );
if ( isVerbose() )
{
getLog().info( "found " + unsafeMappings.size() + " unsafe mappings" );
Expand Down Expand Up @@ -443,7 +445,7 @@ void initFromMojo(AggregatorAddThirdPartyMojo mojo, MavenProject mavenProject, M
missingLicensesFileArtifact = mojo.missingLicensesFileArtifact;
localRepository = mojo.localRepository;
remoteRepositories = mojo.remoteRepositories;
dependencies = new HashSet<Artifact>(mavenProject.getDependencies());
dependencies = new HashSet<>(mavenProject.getDependencies());
licenseMerges = mojo.licenseMerges;
licenseMergesFile = mojo.licenseMergesFile;
includedLicenses = mojo.includedLicenses;
Expand All @@ -462,8 +464,9 @@ void initFromMojo(AggregatorAddThirdPartyMojo mojo, MavenProject mavenProject, M

setLog(mojo.getLog());

dependenciesTool.loadProjectArtifacts( localRepository, project.getRemoteArtifactRepositories(), project ,reactorProjects);

isAggregatorBuild = true;
reactorProjectDependencies = reactorProjects;

init();

consolidate();
Expand Down
Loading

0 comments on commit 2308f69

Please sign in to comment.