Author: tchemit Date: 2010-10-15 11:52:34 +0200 (Fri, 15 Oct 2010) New Revision: 1844 Url: http://nuiton.org/repositories/revision/maven-license-plugin/1844 Log: Anomalie #938: licenseName should be expression license.licenseName Anomalie #939: When license name is not found, a NPE occurs, it should be a nicer message Anomalie #947: Files to update are not found in a multi-module project Modified: trunk/src/main/java/org/nuiton/license/plugin/AbstractLicenseWithDescriptorMojo.java Modified: trunk/src/main/java/org/nuiton/license/plugin/AbstractLicenseWithDescriptorMojo.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/AbstractLicenseWithDescriptorMojo.java 2010-10-15 07:10:24 UTC (rev 1843) +++ trunk/src/main/java/org/nuiton/license/plugin/AbstractLicenseWithDescriptorMojo.java 2010-10-15 09:52:34 UTC (rev 1844) @@ -29,7 +29,12 @@ import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.project.MavenProject; import org.nuiton.license.plugin.header.FileHeader; +import org.nuiton.license.plugin.header.transformer.AptFileHeaderTransformer; import org.nuiton.license.plugin.header.transformer.FileHeaderTransformer; +import org.nuiton.license.plugin.header.transformer.JavaFileHeaderTransformer; +import org.nuiton.license.plugin.header.transformer.PropertiesFileHeaderTransformer; +import org.nuiton.license.plugin.header.transformer.RstFileHeaderTransformer; +import org.nuiton.license.plugin.header.transformer.XmlFileHeaderTransformer; import org.nuiton.license.plugin.model.License; import org.nuiton.license.plugin.model.LicenseStore; import org.nuiton.license.plugin.model.descriptor.FileSet; @@ -150,9 +155,20 @@ /** descriptor of project */ private LicenseProjectDescriptor licenseProjectDescriptor; - /** Map that contains each files indexed by their CommentStyle **/ + /** Map that contains each files indexed by their CommentStyle */ private Map<String, List<File>> universe; + public static final String[] DEFAULT_INCLUDES = new String[]{"**/*"}; + + public static final String[] DEFAULT_EXCLUDES = new String[]{ + "**/*.zargo", "**/*.uml", "**/*.umldi", "**/*.xmi", /* modelisation */ + "**/*.img", "**/*.png", "**/*.jpg", "**/*.jpeg", "**/*.gif", /* images */ + "**/i18n/*.properties", + "**/*.zip" + }; + + public static final String[] DEFAULT_ROOTS = new String[]{"src"}; + /** * When is sets to {@code true}, will skip execution. * <p/> @@ -216,26 +232,39 @@ if (!descriptorFile.exists()) { // No descriptor File: use no descriptor + // check licenseName exists + checkLicense(licenseName); + // Map that will contain all the files, grouped by root directory Map<File, String[]> filesToTreat = new HashMap<File, String[]>(); // Init default value for roots and includes if (roots == null || roots.length == 0) { - roots = new String[]{"src"}; - getLog().info("Will use default roots "+ Arrays.toString(roots)); + roots = DEFAULT_ROOTS; + getLog().info("Will use default roots " + + Arrays.toString(roots)); } - List<String> rootsList = Arrays.asList(getRoots()); +// List<String> rootsList = Arrays.asList(getRoots()); + List<String> rootsList = new ArrayList<String>(roots.length); + for (String root : roots) { + File f = new File(root); + if (f.isAbsolute()) { + rootsList.add(f.getAbsolutePath()); + } else { + rootsList.add(new File(getProject().getBasedir(), root).getAbsolutePath()); + } + } if (includes == null || includes.length == 0) { - includes = new String[]{"**/*"}; + includes = DEFAULT_INCLUDES; + getLog().info("Will use default includes " + + Arrays.toString(includes)); } if (excludes == null || excludes.length == 0) { - excludes = new String[]{ - "**/*.zargo","**/*.uml","**/*.umldi","**/*.xmi", /* modelisation */ - "**/*.img","**/*.png","**/*.jpg","**/*.jpeg","**/*.gif", /* images */ - "**/i18n/*.properties" - }; + excludes = DEFAULT_EXCLUDES; + getLog().info("Will use default excludes" + + Arrays.toString(excludes)); } // Get all files ordered by roots @@ -251,11 +280,11 @@ } // Will use the deprecated license project descriptor - + getLog().warn("\n"); - getLog().warn("----------------------------------------------------------"); - getLog().warn("The use of Descriptor File will be obsolete from version 3"); - getLog().warn("----------------------------------------------------------"); + getLog().warn("-------------------------------------------------------------------------"); + getLog().warn("The use of Descriptor File is deprecated and will be removed in version 3"); + getLog().warn("-------------------------------------------------------------------------"); getLog().warn("\n"); getLog().info("Loading descriptor " + descriptorFile); @@ -288,7 +317,6 @@ this.encoding = encoding; } - public File getDescriptor() { return descriptor; } @@ -393,19 +421,18 @@ } public License getMainLicense() - throws IllegalArgumentException, IllegalStateException { + throws IllegalArgumentException, IllegalStateException, MojoFailureException { LicenseProjectDescriptor licenseProject = getLicenseProjectDescriptor(); if (licenseProject != null) { + + // use the main license from the license project descriptor licenseName = licenseProject.getMainLicense(); } - if (licenseName == null) { - throw new IllegalStateException("No license project initialized!"); - } - if (StringUtils.isEmpty(licenseName)) { - throw new IllegalArgumentException( - "main license name can not be null, nor empty"); - } + // check license exists + checkLicense(licenseName); + + // obtain license from his name License mainLicense = getLicense(licenseName); return mainLicense; } @@ -503,16 +530,11 @@ Map<String, FileHeaderTransformer> transformers) throws MojoFailureException { - List<String> licenseNames = Arrays.asList(licenseStore.getLicenseNames()); - // check licenses is known String mainLicense = licenseProjectDescriptor.getMainLicense(); - if (licenseStore.getLicense(mainLicense) == null) { - throw new MojoFailureException( - "main license '" + mainLicense + - "' is unknown, use one of " + licenseNames); - } + checkLicense(mainLicense); + Header singleHeader = licenseProjectDescriptor.getHeader(); if (singleHeader != null) { @@ -538,11 +560,7 @@ } else { // check license name - if (licenseStore.getLicense(licenseName) == null) { - throw new MojoFailureException( - "license '" + licenseName + - "' is unknown, use one of " + licenseNames); - } + checkLicense(licenseName); } FileSet singleFileSet = header.getFileSet(); @@ -580,14 +598,14 @@ universe = new HashMap<String, List<File>>(); - for (Map.Entry<File, String[]> entry: filesToTreat.entrySet()) { + for (Map.Entry<File, String[]> entry : filesToTreat.entrySet()) { File root = entry.getKey(); String[] filesPath = entry.getValue(); // Foreach file, sort them by CS - for (String path: filesPath) { + for (String path : filesPath) { String[] splitPath = path.split("\\."); - String extension = splitPath[splitPath.length -1]; + String extension = splitPath[splitPath.length - 1]; File file = new File(root, path); String commentStyle = getCommentStyle(extension); @@ -605,21 +623,61 @@ } + /** + * Check if the given license name is valid (not null, nor empty) and + * exists in the license store. + * + * @param licenseName the name of the license to check + * @throws IllegalArgumentException if license is not valid + * @throws IllegalStateException if license store is not initialized + * @throws MojoFailureException if license does not exist + * @since 2.4.1 + */ + protected void checkLicense(String licenseName) + throws IllegalArgumentException, + IllegalStateException, MojoFailureException { + if (StringUtils.isEmpty(licenseName)) { + throw new IllegalArgumentException( + "licenseName can not be null, nor empty"); + } + LicenseStore licenseStore = getLicenseStore(); + if (licenseStore == null) { + throw new IllegalStateException("No license store initialized!"); + } + License mainLicense = licenseStore.getLicense(licenseName); + if (mainLicense == null) { + throw new MojoFailureException( + "License named '" + mainLicense + + "' is unknown, use one of " + + Arrays.toString(licenseStore.getLicenseNames())); + } + } + + /** + * Obtain the known Comment style from the given extension + * + * @param extension the extension to match with a comment style + * @return the known comment style, or null if not found + */ protected String getCommentStyle(String extension) { - if ("java".equals(extension)) { - return "java"; + + // trim the extension + extension = extension.trim(); + + if (JavaFileHeaderTransformer.NAME.equalsIgnoreCase(extension)) { + return JavaFileHeaderTransformer.NAME; } - if ("xml".equals(extension)) { - return "xml"; + if (XmlFileHeaderTransformer.NAME.equalsIgnoreCase(extension)) { + return XmlFileHeaderTransformer.NAME; } - if ("apt".equals(extension)) { - return "apt"; + if (AptFileHeaderTransformer.NAME.equalsIgnoreCase(extension)) { + return AptFileHeaderTransformer.NAME; } - if ("properties".equals(extension)) { - return "properties"; + if (PropertiesFileHeaderTransformer.NAME.equalsIgnoreCase(extension)) { + return PropertiesFileHeaderTransformer.NAME; } - if ("rst".equals(extension)) { - return "rst"; + if (RstFileHeaderTransformer.NAME.equalsIgnoreCase(extension)) { + return RstFileHeaderTransformer.NAME; } return null;
participants (1)
-
tchemit@users.nuiton.org