Author: ymartel Date: 2010-09-24 11:30:43 +0200 (Fri, 24 Sep 2010) New Revision: 1820 Url: http://nuiton.org/repositories/revision/maven-license-plugin/1820 Log: Review code to extract some operation out of loops Modified: trunk/src/main/java/org/nuiton/license/plugin/AbstractAddThirdPartyMojo.java trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyMojo.java Modified: trunk/src/main/java/org/nuiton/license/plugin/AbstractAddThirdPartyMojo.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/AbstractAddThirdPartyMojo.java 2010-09-23 14:56:51 UTC (rev 1819) +++ trunk/src/main/java/org/nuiton/license/plugin/AbstractAddThirdPartyMojo.java 2010-09-24 09:30:43 UTC (rev 1820) @@ -159,6 +159,7 @@ /** * A filter for projects licenses. + * * @parameter expression="${license.projectFilter}" default-value="" * @since 2.3.2 */ @@ -203,9 +204,9 @@ * property {@code file.encoding}. * * @parameter expression="${license.encoding}" default-value="${project.build.sourceEncoding}" - * + * <p/> * tchemit 2010-08-29 Ano #842 - * In maven 3, it will not pass if nothing was filled in project.build.sourceEncoding, + * In maven 3, it will not pass if nothing was filled in project.build.sourceEncoding, * As we have a futher fallback for this case we do not need to be required here. * //@required * @since 2.1 @@ -272,8 +273,8 @@ log.info("bundle third-party file : " + bundleFile.lastModified()); } setDoGenerateBundle(isForce() || - !bundleFile.exists() || - buildTimestamp > bundleFile.lastModified()); + !bundleFile.exists() || + buildTimestamp > bundleFile.lastModified()); } else { // not generating bundled file @@ -285,8 +286,8 @@ unsafeDependencies = licenseMap.getUnsafeDependencies(); if (!CollectionUtils.isEmpty(unsafeDependencies) && - isUseMissingFile() && - isDoGenerate()) { + isUseMissingFile() && + isDoGenerate()) { // load unsafeMapping unsafeMappings = createUnsafeMapping(); @@ -369,7 +370,7 @@ // group by license sb.append("List of third-party dependencies grouped by " + - "their license type."); + "their license type."); for (String licenseName : licenseMap.keySet()) { SortedSet<MavenProject> projects = licenseMap.get(licenseName); sb.append("\n\n").append(licenseName).append(" : "); @@ -547,25 +548,21 @@ this.doGenerateBundle = doGenerateBundle; } - public String getProjectFilter() - { - return projectFilter; - } + public String getProjectFilter() { + return projectFilter; + } - public void setProjectFilter(String projectFilter) - { - this.projectFilter = projectFilter; - } + public void setProjectFilter(String projectFilter) { + this.projectFilter = projectFilter; + } - public String getExcludedGroups() - { - return excludedGroups; - } + public String getExcludedGroups() { + return excludedGroups; + } - public void setExcludedGroups(String excludedGroups) - { - this.excludedGroups = excludedGroups; - } + public void setExcludedGroups(String excludedGroups) { + this.excludedGroups = excludedGroups; + } public String getIncludedGroups() { return includedGroups; Modified: trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyMojo.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyMojo.java 2010-09-23 14:56:51 UTC (rev 1819) +++ trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyMojo.java 2010-09-24 09:30:43 UTC (rev 1820) @@ -105,6 +105,29 @@ LicenseMap licenseMap = new LicenseMap(this.getProjectFilter()); licenseMap.setLog(log); + boolean noIncludedGroups = StringUtils.isEmpty(includedGroups); + boolean noExcludedGroups = StringUtils.isEmpty(excludedGroups); + boolean noIncludedArtifacts = StringUtils.isEmpty(includedArtifacts); + boolean noExcludedArtifacts = StringUtils.isEmpty(excludedArtifacts); + + Pattern includedGroupPattern = null; + Pattern includedArtifactPattern = null; + Pattern excludedGroupPattern = null; + Pattern excludedArtifactPattern = null; + + if (!noIncludedGroups) { + includedGroupPattern = Pattern.compile(includedGroups); + } + if (!noIncludedArtifacts) { + includedArtifactPattern = Pattern.compile(includedArtifacts); + } + if (!noExcludedGroups) { + excludedGroupPattern = Pattern.compile(excludedGroups); + } + if (!noExcludedArtifacts) { + excludedArtifactPattern = Pattern.compile(excludedArtifacts); + } + // build the license map for the dependencies of the project for (Object o : getProject().getArtifacts()) { @@ -120,10 +143,11 @@ MavenProject project = addArtifact(id, artifact); // Check if the project should be included - boolean isToInclude = isIncludable(project); + boolean isToInclude = noIncludedArtifacts && noIncludedGroups + || isIncludable(project, includedGroupPattern, includedArtifactPattern); // Check if the project should be excluded - boolean isToExclude = isExcludable(project); + boolean isToExclude = isExcludable(project, excludedGroupPattern, excludedArtifactPattern); if (isToInclude && !isToExclude) { licenseMap.addLicense(project, project.getLicenses()); @@ -256,66 +280,62 @@ } - protected boolean isIncludable(MavenProject project) { + protected boolean isIncludable(MavenProject project, + Pattern includedGroupPattern, + Pattern includedArtifactPattern) { Log log = getLog(); - // 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)) { + // check if the groupId of the project should be included + if (includedGroupPattern != null) { // 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; + try { + Matcher matchGroupId = includedGroupPattern.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."); - } } + 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)) { + // check if the artifactId of the project should be included + if (includedArtifactPattern != null) { // 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; + try { + Matcher matchGroupId = includedArtifactPattern.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."); - } } + catch (PatternSyntaxException e) { + getLog().warn("The pattern specified by expression <" + + includedArtifacts + "> seems to be invalid."); + } } return false; } - protected boolean isExcludable(MavenProject project) { + protected boolean isExcludable(MavenProject project, + Pattern excludedGroupPattern, + Pattern excludedArtifactPattern) { Log log = getLog(); // check if the groupId of the project should be included - if (StringUtils.isNotEmpty(excludedGroups)) { - // we have some defined license filters + if (excludedGroupPattern != null) { + // we have some defined license filters try { - Pattern pattern = Pattern.compile(excludedGroups); - Matcher matchGroupId = pattern.matcher(project.getGroupId()); + Matcher matchGroupId = excludedGroupPattern.matcher(project.getGroupId()); if (matchGroupId.find()) { if (log.isDebugEnabled()) { log.debug("Exclude " + project.getGroupId()); @@ -330,11 +350,10 @@ } // check if the artifactId of the project should be included - if (StringUtils.isNotEmpty(excludedArtifacts)) { - // we have some defined license filters + if (excludedArtifactPattern != null) { + // we have some defined license filters try { - Pattern pattern = Pattern.compile(excludedArtifacts); - Matcher matchGroupId = pattern.matcher(project.getArtifactId()); + Matcher matchGroupId = excludedArtifactPattern.matcher(project.getArtifactId()); if (matchGroupId.find()) { if (log.isDebugEnabled()) { log.debug("Exclude " + project.getArtifactId());
participants (1)
-
ymartel@users.nuiton.org