View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.maven.plugin;
17  
18  import java.io.File;
19  import java.io.IOException;
20  
21  import org.apache.commons.io.FileUtils;
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.project.MavenProject;
27  import org.kuali.maven.common.Extractor;
28  
29  /**
30   * Update the SCM section of a Maven pom so the URL's match the correct SCM URL. The correct SCM URL to use must be
31   * provided to the plugin as a property.
32   * 
33   * @goal updatescm
34   */
35  public class UpdateScmMojo extends AbstractMojo {
36  
37      /**
38       * The Maven project this plugin runs in.
39       * 
40       * @parameter expression="${project}"
41       * @required
42       * @readonly
43       */
44      private MavenProject project;
45  
46      /**
47       * The property containing the correct SCM URL. This can be a project property, system property, or environment
48       * variable.
49       * 
50       * @parameter expression="${extractor.scmUrlProperty}" default-value="scm.url"
51       * @required
52       */
53      private String scmUrlProperty;
54  
55      /**
56       * The location of the Maven pom to update.
57       * 
58       * @parameter expression="${extractor.pom}" default-value="${project.basedir}/pom.xml"
59       * @required
60       */
61      private File pom;
62  
63      @Override
64      public void execute() throws MojoExecutionException, MojoFailureException {
65          Extractor extractor = new Extractor();
66          String pomUrl = extractor.getScmUrl(project.getScm());
67          String actualUrl = extractor.getActualUrl(project, scmUrlProperty);
68          if (pomUrl.equals(actualUrl)) {
69              getLog().info("pom url matches actual url.  Nothing to do!");
70              return;
71          }
72          try {
73              String content = FileUtils.readFileToString(pom);
74              content = handleConnection(content, "connection", pomUrl, actualUrl);
75              content = handleConnection(content, "developerConnection", pomUrl, actualUrl);
76              content = handleConnection(content, "url", pomUrl, actualUrl);
77              FileUtils.writeStringToFile(pom, content);
78          } catch (IOException e) {
79              throw new MojoExecutionException("Unexpected IO exception", e);
80          }
81      }
82  
83      protected String handleConnection(String content, String tag, String pomUrl, String newUrl) {
84          String scm = StringUtils.substringBetween(content, "<scm>", "</scm>");
85          getLog().debug("scm=" + scm);
86          String open = "<" + tag + ">";
87          String close = "</" + tag + ">";
88          String oldScm = open + StringUtils.substringBetween(scm, open, close) + close;
89          getLog().debug("oldScm=" + oldScm);
90          getLog().debug("pomUrl=        " + pomUrl);
91          int pos = oldScm.indexOf(pomUrl);
92          if (pos == -1) {
93              throw new IllegalStateException("Existing SCM information doesn't contain " + pomUrl);
94          }
95          String newScm = oldScm.replace(pomUrl, newUrl);
96          getLog().debug("Old=[" + oldScm + "]");
97          getLog().info("Update - " + newScm);
98          return content.replace(oldScm, newScm);
99      }
100 
101     public MavenProject getProject() {
102         return project;
103     }
104 
105     public String getScmUrlProperty() {
106         return scmUrlProperty;
107     }
108 
109     public void setScmUrlProperty(String svnUrlProperty) {
110         this.scmUrlProperty = svnUrlProperty;
111     }
112 }