Author: ymartel Date: 2010-09-23 16:53:27 +0200 (Thu, 23 Sep 2010) New Revision: 1818 Url: http://nuiton.org/repositories/revision/maven-license-plugin/1818 Log: Add properties to exclude artifacts from Third Party reports, and to include only some groups or/and artifacts Modified: trunk/src/it/excluded-included/pom.xml trunk/src/it/excluded-included/verify.groovy trunk/src/main/java/org/nuiton/license/plugin/AbstractAddThirdPartyMojo.java trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyMojo.java Modified: trunk/src/it/excluded-included/pom.xml =================================================================== --- trunk/src/it/excluded-included/pom.xml 2010-09-23 11:04:09 UTC (rev 1817) +++ trunk/src/it/excluded-included/pom.xml 2010-09-23 14:53:27 UTC (rev 1818) @@ -65,17 +65,65 @@ <thirdPartyFilename>thirdWithoutGroup.txt</thirdPartyFilename> </configuration> </execution> - <!--<execution>--> - <!--<id>artifact-filter</id>--> - <!--<goals>--> - <!--<goal>add-third-party</goal>--> - <!--</goals>--> - <!--<phase>validate</phase>--> - <!--<configuration>--> - <!--<projectFilter>nuiton-i18n</projectFilter>--> - <!--<thirdPartyFilename>thirdWithoutArtifact.txt</thirdPartyFilename>--> - <!--</configuration>--> - <!--</execution>--> + <execution> + <id>artifact-filter</id> + <goals> + <goal>add-third-party</goal> + </goals> + <phase>validate</phase> + <configuration> + <excludedArtifacts>nuiton-i18n</excludedArtifacts> + <thirdPartyFilename>thirdWithoutArtifact.txt</thirdPartyFilename> + </configuration> + </execution> + <execution> + <id>artifact-off-over-group-on</id> + <goals> + <goal>add-third-party</goal> + </goals> + <phase>validate</phase> + <configuration> + <includedGroups>org\.nuiton</includedGroups> + <excludedArtifacts>nuiton-utils|nuiton-i18n</excludedArtifacts> + <thirdPartyFilename>thirdWithGroupWithoutArtifact.txt</thirdPartyFilename> + </configuration> + </execution> + <execution> + <id>artifact-on-over-group-off</id> + <goals> + <goal>add-third-party</goal> + </goals> + <phase>validate</phase> + <configuration> + <excludedGroups>org\.nuiton</excludedGroups> + <includedArtifacts>nuiton-utils|nuiton-i18n</includedArtifacts> + <thirdPartyFilename>thirdWithoutGroupWithArtifact.txt</thirdPartyFilename> + </configuration> + </execution> + <execution> + <id>artifact-on-over-group-on</id> + <goals> + <goal>add-third-party</goal> + </goals> + <phase>validate</phase> + <configuration> + <includedGroups>org\.nuiton</includedGroups> + <includedArtifacts>nuiton-utils</includedArtifacts> + <thirdPartyFilename>thirdWithGroupWithArtifact.txt</thirdPartyFilename> + </configuration> + </execution> + <execution> + <id>artifact-off-over-group-off</id> + <goals> + <goal>add-third-party</goal> + </goals> + <phase>validate</phase> + <configuration> + <excludedGroups>org\.nuiton</excludedGroups> + <excludedArtifacts>nuiton-i18n</excludedArtifacts> + <thirdPartyFilename>thirdWithoutGroupWithoutArtifact.txt</thirdPartyFilename> + </configuration> + </execution> </executions> </plugin> </plugins> Modified: trunk/src/it/excluded-included/verify.groovy =================================================================== --- trunk/src/it/excluded-included/verify.groovy 2010-09-23 11:04:09 UTC (rev 1817) +++ trunk/src/it/excluded-included/verify.groovy 2010-09-23 14:53:27 UTC (rev 1818) @@ -4,7 +4,56 @@ content = file.text; assert !content.contains( 'the project has no dependencies.' ); assert content.contains( 'commons-logging:commons-logging:1.1.1' ); -assert !content.contains( 'org.nuiton:nuiton-util:1.4' ); +assert !content.contains( 'org.nuiton:nuiton-utils:1.4' ); assert !content.contains( 'org.nuiton.i18n:nuiton-i18n:1.2.2' ); + +file = new File(basedir, 'target/generated-sources/license/thirdWithoutArtifact.txt'); +assert file.exists(); +content = file.text; +assert !content.contains( 'the project has no dependencies.' ); +assert content.contains( 'commons-logging:commons-logging:1.1.1' ); +assert content.contains( 'org.nuiton:nuiton-utils:1.4' ); +assert !content.contains( 'org.nuiton.i18n:nuiton-i18n:1.2.2' ); + + +file = new File(basedir, 'target/generated-sources/license/thirdWithGroupWithoutArtifact.txt'); +assert file.exists(); +content = file.text; +assert !content.contains( 'the project has no dependencies.' ); +assert !content.contains( 'commons-logging:commons-logging:1.1.1' ); +assert !content.contains( 'org.nuiton:nuiton-utils:1.4' ); +assert !content.contains( 'org.nuiton.i18n:nuiton-i18n:1.2.2' ); +assert content.contains( 'org.nuiton:maven-helper-plugin' ); + + +file = new File(basedir, 'target/generated-sources/license/thirdWithoutGroupWithArtifact.txt'); +assert file.exists(); +content = file.text; +assert content.contains( 'the project has no dependencies.' ); +assert !content.contains( 'commons-logging:commons-logging:1.1.1' ); +assert !content.contains( 'org.nuiton:nuiton-utils:1.4' ); +assert !content.contains( 'org.nuiton.i18n:nuiton-i18n:1.2.2' ); +assert !content.contains( 'org.nuiton:maven-helper-plugin' ); + + +file = new File(basedir, 'target/generated-sources/license/thirdWithGroupWithArtifact.txt'); +assert file.exists(); +content = file.text; +assert !content.contains( 'the project has no dependencies.' ); +assert !content.contains( 'commons-logging:commons-logging:1.1.1' ); +assert content.contains( 'org.nuiton:nuiton-utils:1.4' ); +assert content.contains( 'org.nuiton.i18n:nuiton-i18n:1.2.2' ); +assert content.contains( 'org.nuiton:maven-helper-plugin' ); + + +file = new File(basedir, 'target/generated-sources/license/thirdWithoutGroupWithoutArtifact.txt'); +assert file.exists(); +content = file.text; +assert !content.contains( 'the project has no dependencies.' ); +assert content.contains( 'commons-logging:commons-logging:1.1.1' ); +assert !content.contains( 'org.nuiton:nuiton-utils:1.4' ); +assert !content.contains( 'org.nuiton.i18n:nuiton-i18n:1.2.2' ); +assert !content.contains( 'org.nuiton:maven-helper-plugin' ); + return true; Modified: trunk/src/main/java/org/nuiton/license/plugin/AbstractAddThirdPartyMojo.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/AbstractAddThirdPartyMojo.java 2010-09-23 11:04:09 UTC (rev 1817) +++ trunk/src/main/java/org/nuiton/license/plugin/AbstractAddThirdPartyMojo.java 2010-09-23 14:53:27 UTC (rev 1818) @@ -173,6 +173,30 @@ protected String excludedGroups; /** + * A filter to include only some GroupIds + * + * @parameter expression="${license.includedGroups}" default-value="" + * @since 2.3.2 + */ + protected String includedGroups; + + /** + * A filter to exclude some ArtifactsIds + * + * @parameter expression="${license.excludedArtifacts}" default-value="" + * @since 2.3.2 + */ + protected String excludedArtifacts; + + /** + * A filter to include only some ArtifactsIds + * + * @parameter expression="${license.includedArtifacts}" default-value="" + * @since 2.3.2 + */ + protected String includedArtifacts; + + /** * Encoding used to read and writes files. * <p/> * <b>Note:</b> If nothing is filled here, we will use the system @@ -359,6 +383,7 @@ } else { + // group by dependencies SortedMap<MavenProject, String[]> map = licenseMap.toDependencyMap(); @@ -541,4 +566,28 @@ { this.excludedGroups = excludedGroups; } + + public String getIncludedGroups() { + return includedGroups; + } + + public void setIncludedGroups(String includedGroups) { + this.includedGroups = includedGroups; + } + + public String getExcludedArtifacts() { + return excludedArtifacts; + } + + public void setExcludedArtifacts(String excludedArtifacts) { + this.excludedArtifacts = excludedArtifacts; + } + + public String getIncludedArtifacts() { + return includedArtifacts; + } + + public void setIncludedArtifacts(String includedArtifacts) { + this.includedArtifacts = includedArtifacts; + } } \ No newline at end of file Modified: trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyMojo.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyMojo.java 2010-09-23 11:04:09 UTC (rev 1817) +++ trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyMojo.java 2010-09-23 14:53:27 UTC (rev 1818) @@ -28,6 +28,7 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.SortedSet; @@ -105,7 +106,6 @@ licenseMap.setLog(log); // build the license map for the dependencies of the project - for (Object o : getProject().getArtifacts()) { Artifact artifact = (Artifact) o; @@ -119,26 +119,17 @@ MavenProject project = addArtifact(id, artifact); - // filter with the excluded groupIds - if (StringUtils.isNotEmpty(excludedGroups)) { - // we have some defined license filters - try { - Pattern pattern = Pattern.compile(excludedGroups); - Matcher matchGroupId = pattern.matcher(project.getGroupId()); - if (!matchGroupId.find()) { - if (log.isDebugEnabled()) { - log.debug("Exclude " + project.getGroupId()); - } - licenseMap.addLicense(project, project.getLicenses()); - } - } - catch (PatternSyntaxException e) { - getLog().warn("The pattern specified by expression <" + excludedGroups + "> seems to be invalid."); - } - } else { + // Check if the project should be included + boolean isToInclude = isIncludable(project, log); + + // Check if the project should be excluded + boolean isToExclude = isExcludable(project, log); + + if (isToInclude && !isToExclude) { licenseMap.addLicense(project, project.getLicenses()); } } + return licenseMap; } @@ -264,4 +255,97 @@ this.doGenerateMissing = doGenerateMissing; } + + protected boolean isIncludable(MavenProject project, Log log) { + + // If there is no constraint on groups or artifacts, all should be included + if(StringUtils.isEmpty(includedGroups) && StringUtils.isEmpty(includedArtifacts)) { + return true; + + } else { + // check if the groupId of the project should be included + if (StringUtils.isNotEmpty(includedGroups)) { + // we have some defined license filters + try { + Pattern pattern = Pattern.compile(includedGroups); + Matcher matchGroupId = pattern.matcher(project.getGroupId()); + if (matchGroupId.find()) { + if (log.isDebugEnabled()) { + log.debug("Include " + project.getGroupId()); + } + return true; + } + } + catch (PatternSyntaxException e) { + getLog().warn("The pattern specified by expression <" + + includedGroups + "> seems to be invalid."); + } + } + + // check if the artifactId of the project should be included + if (StringUtils.isNotEmpty(includedArtifacts)) { + // we have some defined license filters + try { + Pattern pattern = Pattern.compile(includedArtifacts); + Matcher matchGroupId = pattern.matcher(project.getArtifactId()); + if (matchGroupId.find()) { + if (log.isDebugEnabled()) { + log.debug("Include " + project.getArtifactId()); + } + return true; + } + } + catch (PatternSyntaxException e) { + getLog().warn("The pattern specified by expression <" + + includedArtifacts + "> seems to be invalid."); + } + } + } + + return false; + } + + + protected boolean isExcludable(MavenProject project, Log log) { + + // check if the groupId of the project should be included + if (StringUtils.isNotEmpty(excludedGroups)) { + // we have some defined license filters + try { + Pattern pattern = Pattern.compile(excludedGroups); + Matcher matchGroupId = pattern.matcher(project.getGroupId()); + if (matchGroupId.find()) { + if (log.isDebugEnabled()) { + log.debug("Exclude " + project.getGroupId()); + } + return true; + } + } + catch (PatternSyntaxException e) { + getLog().warn("The pattern specified by expression <" + + excludedGroups + "> seems to be invalid."); + } + } + + // check if the artifactId of the project should be included + if (StringUtils.isNotEmpty(excludedArtifacts)) { + // we have some defined license filters + try { + Pattern pattern = Pattern.compile(excludedArtifacts); + Matcher matchGroupId = pattern.matcher(project.getArtifactId()); + if (matchGroupId.find()) { + if (log.isDebugEnabled()) { + log.debug("Exclude " + project.getArtifactId()); + } + return true; + } + } + catch (PatternSyntaxException e) { + getLog().warn("The pattern specified by expression <" + + excludedArtifacts + "> seems to be invalid."); + } + } + + return false; + } }