1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33
34
35 public class UpdateScmMojo extends AbstractMojo {
36
37
38
39
40
41
42
43
44 private MavenProject project;
45
46
47
48
49
50
51
52
53 private String scmUrlProperty;
54
55
56
57
58
59
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 }