r87 - in trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin: . announcement
Author: tchemit Date: 2010-01-18 02:18:09 +0100 (Mon, 18 Jan 2010) New Revision: 87 Modified: trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/AbstractRedmineMojo.java trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/GenerateChangesMojo.java trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/NextVersionMojo.java trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishAttachmentsMojo.java trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishNewsMojo.java trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/UpdateVersionMojo.java trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AbstractAnnouncementMojo.java trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AnnouncementGenerator.java Log: clean code + improve announcement generator code + avoid calling redmine server when not required Modified: trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/AbstractRedmineMojo.java =================================================================== --- trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/AbstractRedmineMojo.java 2010-01-17 20:14:27 UTC (rev 86) +++ trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/AbstractRedmineMojo.java 2010-01-18 01:18:09 UTC (rev 87) @@ -189,7 +189,6 @@ /////////////////////////////////////////////////////////////////////////// @Override protected void init() throws Exception { -// protected boolean init() throws Exception { // check issue management @@ -197,19 +196,10 @@ if (issueManagement == null) { throw new MojoExecutionException("No Issue Management set."); -// getLog().error("No Issue Management set."); -// -// return false; } else if ((issueManagement.getUrl() == null) || (issueManagement.getUrl().trim().equals(""))) { throw new MojoExecutionException("No URL set in Issue Management."); -// getLog().error("No URL set in Issue Management."); -// -// return false; } else if ((issueManagement.getSystem() != null) && !(issueManagement.getSystem().equalsIgnoreCase(AbstractRedmineMojo.REDMINE_SYSTEM))) { throw new MojoExecutionException("Redmine's Plugin only supports 'redmine' Issue Management system."); -// getLog().error("Redmine's Plugin only supports 'redmine' Issue Management system."); -// -// return false; } // prepare Redmine service configuration @@ -251,7 +241,6 @@ // if (verbose) { getLog().error("could not init Redmine service [" + getRestUrl() + "] with user '" + getRestUsername() + "'", e); // } -// return false; } // check project exists @@ -265,10 +254,6 @@ initOk = false; return; } -// if (!r) { -// getLog().error("the project '" + projectId + "' could not be retrieve from redmine server, goal is skip"); -// return false; -// } } // check user exists @@ -282,10 +267,6 @@ initOk = false; return; } -// if (!r) { -// getLog().error("the user '" + this.username + "' could not be retrieve from redmine server, goal is skip"); -// return false; -// } } // check version exists @@ -298,13 +279,7 @@ } initOk = false; } -// if (!r) { -// getLog().error("the version '" + versionId + "' could not be retrieve from redmine server, goal is skip"); -// return false; -// } } - -// return true; } @Override Modified: trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java =================================================================== --- trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java 2010-01-17 20:14:27 UTC (rev 86) +++ trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java 2010-01-18 01:18:09 UTC (rev 87) @@ -89,8 +89,6 @@ // protected boolean init() throws Exception { super.init(); -// boolean init = super.init(); -// if (init) { if (!safe && !initOk) { // we are in none safe mode but init is not ok... return; @@ -104,13 +102,6 @@ RedmineModelEnum.tracker, RedmineModelEnum.user); -// if (actions.isEmpty()) { -// // no data to treate -// getLog().warn("no data types detected, you must fill the required parameter dataTypes"); -// init = false; -// } -// } -// return init; } @Override Modified: trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/GenerateChangesMojo.java =================================================================== --- trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/GenerateChangesMojo.java 2010-01-17 20:14:27 UTC (rev 86) +++ trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/GenerateChangesMojo.java 2010-01-18 01:18:09 UTC (rev 87) @@ -193,7 +193,6 @@ @Override protected void init() throws Exception { -// protected boolean init() throws Exception { if (xmlPath == null || xmlPath.getAbsolutePath().trim().isEmpty()) { throw new MojoExecutionException("required a xmlPath parameter"); @@ -211,7 +210,6 @@ runOnceDone = checkRunOnceDone(); if (runOnceDone) { return; -// return true; } } @@ -220,13 +218,7 @@ // we are in none safe mode but init is not ok... return; } -// boolean result = super.init(); -// if (!result) { -// return false; -// } -// try { - // get trackers Tracker[] releaseTrackers; @@ -402,16 +394,6 @@ if (!onlyCurrentVersion) { filters.put("version", buffer.substring(1)); } - -// } catch (Exception e) { -// if (verbose) { -// getLog().warn("could not init the plugin for reason " + e.getMessage(), e); -// } else { -// getLog().warn("could not init the plugin for reason " + e.getMessage()); -// } -//// result = false; -// } -// return result; } @Override @@ -438,25 +420,7 @@ File xmlParent = xmlPath.getParentFile(); createDirectoryIfNecessary(xmlParent); -// if (!xmlParent.exists()) { -// xmlParent.mkdirs(); -// } -// if (isRunOnceDone()) { -// -//// if (!xmlPath.exists()) { -// // just copy the already generated changes.xml fil -// -// getLog().info("Use already generated " + xmlPath.getName() + " (" + cacheChangesFile + ")"); -// -// copyFile(cacheChangesFile, xmlPath); -//// } else { -//// -//// getLog().info("skip goal, work already done."); -//// } -// return; -// } - if (releaseVersion.getEffectiveDate() == null) { getLog().debug("The version " + versionId + " is not effective on redmine, should update effective-date property to today"); Modified: trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/NextVersionMojo.java =================================================================== --- trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/NextVersionMojo.java 2010-01-17 20:14:27 UTC (rev 86) +++ trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/NextVersionMojo.java 2010-01-18 01:18:09 UTC (rev 87) @@ -117,19 +117,8 @@ return isRunOnce() && !isExecutionRoot(); } -// @Override -// protected boolean checkSkip() { -// if (isRunOnceDone()) { -// getLog().info("runOnce flag is on and goal was already executed, will skip the goal."); -// return false; -// } -// boolean b = super.checkSkip(); -// return b; -// } - @Override protected void init() throws Exception { -// protected boolean init() throws Exception { if (versionId == null || versionId.trim().isEmpty()) { throw new MojoExecutionException("required a versionId parameter"); @@ -143,7 +132,6 @@ runOnceDone = checkRunOnceDone(); if (runOnceDone) { return; -// return true; } } @@ -155,15 +143,10 @@ } } super.init(); -// return super.init(); } @Override protected void doAction() throws Exception { -// if (isRunOnceDone()) { -// getLog().info("skip goal, runOnce flag is on, and was already executed."); -// return; -// } if (dryRun) { getLog().info("\n dryRun flag is on, no data will be send!\n"); @@ -209,6 +192,5 @@ service.nextVersion(projectId, usePreviousVersion ? previousVersionName : null, releaseVersion); } - } } Modified: trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishAttachmentsMojo.java =================================================================== --- trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishAttachmentsMojo.java 2010-01-17 20:14:27 UTC (rev 86) +++ trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishAttachmentsMojo.java 2010-01-18 01:18:09 UTC (rev 87) @@ -111,31 +111,9 @@ @Override protected void init() throws Exception { -// protected boolean init() throws Exception { versionId = PluginHelper.removeSnapshotSuffix(versionId); - runOnceDone = false; - - if (isRunOnce()) { - runOnceDone = checkRunOnceDone(); - if (runOnceDone) { - return; -// return true; - } - } - - super.init(); - if (!safe && !initOk) { - // we are in none safe mode but init is not ok... - return; - } - -// if (!super.init()) { -// -// return false; -// } - if (filesFromProperties != null && !filesFromProperties.exists()) { // no file to publish... getLog().warn("could not find the properties file " + filesFromProperties); @@ -145,8 +123,6 @@ if (filesFromProperties != null && files != null && files.length > 0) { throw new MojoExecutionException("can not use both sources files and filesFromProperties"); -// getLog().warn("can not use both sources files and filesFromProperties"); -// return false; } if (filesFromProperties != null) { @@ -157,42 +133,41 @@ allFiles = Arrays.asList(files); } -// if (allFiles.isEmpty()) { -// getLog().warn("No files to send."); -// getLog().warn("will skip publish news to redmine..."); -// return false; + if (allFiles == null || allFiles.isEmpty()) { + return; + } + + runOnceDone = false; + + if (isRunOnce()) { + runOnceDone = checkRunOnceDone(); + if (runOnceDone) { + return; + } + } + + super.init(); +// if (!safe && !initOk) { +// // we are in none safe mode but init is not ok... +// return; // } -// return true; } @Override protected boolean checkSkip() { -// if (isRunOnceDone()) { -// getLog().info("runOnce flag is on and goal was already executed, will skip the goal."); -// return false; -// } - boolean b = super.checkSkip(); - if (!b) { - return false; - } - if (allFiles == null || allFiles.isEmpty()) { getLog().warn("No attachment to publish, will skip the goal."); return false; } - return true; + boolean b = super.checkSkip(); + return b; } @Override protected void doAction() throws Exception { -// if (isRunOnceDone()) { -// getLog().info("Skipping goal, runOnce flag is on and goal was already executed."); -// return; -// } - if (dryRun) { getLog().info("\n dryRun flag is on, no data will be send!\n"); } Modified: trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishNewsMojo.java =================================================================== --- trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishNewsMojo.java 2010-01-17 20:14:27 UTC (rev 86) +++ trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishNewsMojo.java 2010-01-18 01:18:09 UTC (rev 87) @@ -104,7 +104,6 @@ @Override protected void init() throws Exception { -// protected boolean init() throws Exception { runOnceDone = false; @@ -112,20 +111,9 @@ runOnceDone = checkRunOnceDone(); if (runOnceDone) { return; -// return true; } } - super.init(); - if (!safe && !initOk) { - // we are in none safe mode but init is not ok... - return; - } - -// if (!super.init()) { -// return false; -// } - if (newsSummary == null || newsSummary.trim().isEmpty()) { newsSummary = project.getUrl(); } @@ -133,8 +121,6 @@ if (!newsContentFile.exists()) { // no file to publish... throw new MojoExecutionException("could not find the template " + newsContentFile); -// getLog().warn("could not find the template " + newsContentFile); -// return false; } newsTitle = newsTitle.trim(); @@ -144,27 +130,17 @@ getLog().warn("will use the restricted title : " + newsTitle); } -// return true; - } - -// @Override -// protected boolean checkSkip() { -// if (isRunOnceDone()) { -// getLog().info("runOnce flag is on and goal was already executed, will skip the goal."); -// return false; + super.init(); +// if (!safe && !initOk) { +// // we are in none safe mode but init is not ok... +// return; // } -// boolean b = super.checkSkip(); -// return b; -// } + } + @Override protected void doAction() throws Exception { -// if (isRunOnceDone()) { -// getLog().info("skip goal, runOnce flag is on, and was already executed."); -// return; -// } - if (dryRun) { getLog().info("\n dryRun flag is on, no data will be send!\n"); } Modified: trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/UpdateVersionMojo.java =================================================================== --- trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/UpdateVersionMojo.java 2010-01-17 20:14:27 UTC (rev 86) +++ trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/UpdateVersionMojo.java 2010-01-18 01:18:09 UTC (rev 87) @@ -114,7 +114,6 @@ @Override protected void init() throws Exception { -// protected boolean init() throws Exception { if (versionId == null || versionId.trim().isEmpty()) { throw new MojoExecutionException("required a versionId parameter"); @@ -128,7 +127,6 @@ runOnceDone = checkRunOnceDone(); if (runOnceDone) { return; -// return true; } } @@ -143,25 +141,10 @@ } super.init(); -// return super.init(); } -// @Override -// protected boolean checkSkip() { -// if (isRunOnceDone()) { -// getLog().info("skip goal, runOnce flag is on, and was already executed."); -// return false; -// } -// boolean b = super.checkSkip(); -// return b; -// } - @Override protected void doAction() throws Exception { -// if (isRunOnceDone()) { -// getLog().info("skip goal, runOnce flag is on, and was already executed."); -// return; -// } if (dryRun) { getLog().info("\n dryRun flag is on, no data will be send!\n"); Modified: trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AbstractAnnouncementMojo.java =================================================================== --- trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AbstractAnnouncementMojo.java 2010-01-17 20:14:27 UTC (rev 86) +++ trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AbstractAnnouncementMojo.java 2010-01-18 01:18:09 UTC (rev 87) @@ -33,11 +33,8 @@ import java.io.File; import java.util.Date; -import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * @author chemit @@ -201,7 +198,7 @@ protected AnnouncementGenerator generator; - private static final Pattern ARTIFACT_PATTERN = Pattern.compile("(.+)?--(.+)?"); +// private static final Pattern ARTIFACT_PATTERN = Pattern.compile("(.+)?--(.+)?"); /** * @return the name fo the template to use @@ -254,16 +251,20 @@ if (StringUtils.isEmpty(templateEncoding)) { templateEncoding = ReaderFactory.FILE_ENCODING; getLog().warn( - "File encoding has not been set, using platform encoding " + templateEncoding + ", i.e. build is platform dependent!"); + "File encoding has not been set, using platform encoding " + + templateEncoding + ", i.e. build is platform dependent!"); } if (StringUtils.isEmpty(introduction)) { introduction = project.getUrl(); } + generator = new AnnouncementGenerator(this); + if (artifactsFile != null) { if (!artifactsFile.exists()) { - throw new MojoExecutionException("The artifactsFile [" + artifactsFile + "] does not exists"); + throw new MojoExecutionException("The artifactsFile [" + + artifactsFile + "] does not exists"); } @@ -279,35 +280,11 @@ getLog().info("Deploy url = " + url); } - artifactUrls = new HashMap<File, String>(); - - for (File f : files) { - - String artifactInfo = f.getParentFile().getName(); - Matcher matcher = ARTIFACT_PATTERN.matcher(artifactInfo); - - if (!matcher.matches()) { - getLog().error("no matching for file " + artifactInfo + " (" + f + ")"); - } - String artifactId = matcher.group(2); - String groupId = matcher.group(1).replaceAll("\\.", "/") + "/" + artifactId; - String filename = f.getName(); - if ("pom.xml".equals(filename)) { - filename = artifactId + "-" + versionId + ".pom"; - f = new File(filename); - } - String spec = url + groupId + "/" + versionId + "/" + filename; - if (isVerbose()) { - getLog().info("artifact file " + f.getName() + " --> " + spec); - } - artifactUrls.put(f, spec); - } + artifactUrls = generator.getArtifactsUrls(url,versionId,isVerbose(),files); } super.init(); - generator = new AnnouncementGenerator(this); - Attachment[] attachments = service.getAttachments(projectId, versionId); if (attachments.length == 0) { @@ -321,8 +298,8 @@ attachmentUrls = generator.getAttachmentsUrls(attachments); } - - if (isVerbose()) { + + if (isVerbose() && attachmentUrls != null) { for (Map.Entry<Attachment, String> e : attachmentUrls.entrySet()) { Attachment key = e.getKey(); String value = e.getValue(); Modified: trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AnnouncementGenerator.java =================================================================== --- trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AnnouncementGenerator.java 2010-01-17 20:14:27 UTC (rev 86) +++ trunk/maven-jredmine-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AnnouncementGenerator.java 2010-01-18 01:18:09 UTC (rev 87) @@ -29,6 +29,7 @@ import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.context.Context; import org.apache.velocity.exception.ResourceNotFoundException; +import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.StringUtils; import org.nuiton.jredmine.model.Attachment; import org.nuiton.plugin.PluginHelper; @@ -37,10 +38,9 @@ import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Writer; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * @author chemit @@ -57,6 +57,8 @@ */ private static final String ATTACHMENT_TOKEN = "%FILE%"; + private static final Pattern ARTIFACT_PATTERN = Pattern.compile("(.+)?--(.+)?"); + private final AnnouncementGeneratorConfiguration configuration; public AnnouncementGenerator(AnnouncementGeneratorConfiguration configuration) { @@ -85,7 +87,7 @@ if (hasAttachmentLinks) { - urls = new HashMap<Attachment, String>(); + urls = new LinkedHashMap<Attachment, String>(); for (Attachment a : attachments) { String u = parseAttachmentLink(a.getId() + ""); urls.put(a, u); @@ -97,6 +99,96 @@ return urls; } + private class ArtifactInfo { + private File src; + private String groupId; + private String artifactId; + private String url; + private String extension; + private String filename; + + public ArtifactInfo(String url, String versionId, File srcfile) { + + String artifactInfo = srcfile.getParentFile().getName(); + Matcher matcher = ARTIFACT_PATTERN.matcher(artifactInfo); + + if (!matcher.matches()) { + getLog().error("no matching for file " + artifactInfo + " (" + srcfile + ")"); + } + artifactId = matcher.group(2); + groupId = matcher.group(1).replaceAll("\\.", "/") + "/" + artifactId; + String filename = srcfile.getName(); + if ("pom.xml".equals(filename)) { + filename = artifactId + "-" + versionId + ".pom"; + src = new File(filename); + } else { + src = srcfile; + } + this.url = url + groupId + "/" + versionId + "/" + filename; + + this.extension = FileUtils.getExtension(src.getName()); + this.filename = src.getName().substring(0, this.extension.length() - 1); + } + } + + protected class ArtifactComparator implements Comparator<ArtifactInfo> { + + final List<String> groupIds; + + public ArtifactComparator(List<String> groupIds) { + this.groupIds = groupIds; + } + + @Override + public int compare(ArtifactInfo o1, ArtifactInfo o2) { + // sort by groupId + int result = groupIds.indexOf(o1.groupId) - groupIds.indexOf(o2.groupId); + if (result != 0) { + return result; + } + result = o1.extension.compareTo(o2.extension); + if (result != 0) { + if ("pom".equals(o1.extension)) { + return -1; + } + if ("pom".equals(o2.extension)) { + return 1; + } + return result; + } + // sort on filename + result = o1.filename.compareTo(o2.filename); + return result; + } + } + + public Map<File, String> getArtifactsUrls(String url, String versionId, boolean verbose, File[] files) { + Map<File, String> urls = new LinkedHashMap<File, String>(); + List<String> groupIds = new ArrayList<String>(); + List<ArtifactInfo> list = new ArrayList<ArtifactInfo>(); + + for (File f : files) { + ArtifactInfo artifactInfo = new ArtifactInfo(url, versionId, f); + list.add(artifactInfo); + if (!groupIds.contains(artifactInfo.groupId)) { + if (verbose) { + getLog().info("detected groupId " + artifactInfo.groupId); + } + groupIds.add(artifactInfo.groupId); + } + } + + Collections.sort(list, new ArtifactComparator(groupIds)); + + for (ArtifactInfo a : list) { + if (verbose) { + getLog().info("artifact file " + a.src.getName() + " --> " + a.url); + } + urls.put(a.src, a.url); + } + return urls; + } + public Context createVelocityContext(List<?> releases) throws MojoExecutionException { // prepare velocity context @@ -211,7 +303,6 @@ */ public Release getLatestRelease(List<?> releases, String releaseVersion) throws MojoExecutionException { - boolean isFound; Release release; @@ -226,7 +317,6 @@ } if (release.getVersion() != null && release.getVersion().equals(pomVersion)) { - isFound = true; if (getLog().isDebugEnabled()) { getLog().debug("Found the correct release: " + release.getVersion()); logRelease(release); @@ -236,7 +326,7 @@ } release = getRelease(releases, pomVersion); - isFound = (release != null); + boolean isFound = (release != null); if (!isFound) { throw new MojoExecutionException("Couldn't find the release '" + pomVersion + "' among the supplied releases.");
participants (1)
-
tchemit@users.nuiton.org