1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm.plugin;
20
21 import javax.inject.Inject;
22
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugins.annotations.Execute;
28 import org.apache.maven.plugins.annotations.LifecyclePhase;
29 import org.apache.maven.plugins.annotations.Mojo;
30 import org.apache.maven.plugins.annotations.Parameter;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.scm.manager.ScmManager;
33 import org.apache.maven.scm.provider.svn.AbstractSvnScmProvider;
34 import org.apache.maven.settings.crypto.SettingsDecrypter;
35
36
37
38
39
40
41
42 @Mojo(name = "validate", requiresProject = false)
43 @Execute(phase = LifecyclePhase.VALIDATE)
44 public class ValidateMojo extends AbstractScmMojo {
45
46
47
48 @Parameter(property = "scmConnection", defaultValue = "${project.scm.connection}")
49 private String scmConnection;
50
51 @Parameter(defaultValue = "${project}", readonly = true)
52 private MavenProject project;
53
54
55
56
57 @Parameter(property = "scmDeveloperConnection", defaultValue = "${project.scm.developerConnection}")
58 private String scmDeveloperConnection;
59
60
61
62
63
64
65 @Parameter(property = "scmCheckWorkingDirectoryUrl", defaultValue = "false")
66 private boolean scmCheckWorkingDirectoryUrl;
67
68 @Inject
69 public ValidateMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
70 super(manager, settingsDecrypter);
71 }
72
73
74
75
76 public void execute() throws MojoExecutionException {
77 super.execute();
78
79
80 try {
81 validateConnection(getConnectionUrl(), "connectionUrl");
82 } catch (NullPointerException e) {
83
84 }
85
86
87 if (scmConnection != null) {
88 validateConnection(scmConnection, "project.scm.connection");
89 }
90
91
92 if (scmDeveloperConnection != null) {
93 validateConnection(scmDeveloperConnection, "project.scm.developerConnection");
94 }
95 }
96
97 private void validateConnection(String connectionString, String type) throws MojoExecutionException {
98 if (scmCheckWorkingDirectoryUrl) {
99 System.setProperty(
100 AbstractSvnScmProvider.CURRENT_WORKING_DIRECTORY,
101 project.getFile().getParentFile().getAbsolutePath());
102 }
103 List<String> messages = getScmManager().validateScmRepository(connectionString);
104
105 if (!messages.isEmpty()) {
106 getLog().error("Validation of scm url connection (" + type + ") failed :");
107
108 Iterator<String> iter = messages.iterator();
109
110 while (iter.hasNext()) {
111 getLog().error(iter.next().toString());
112 }
113
114 getLog().error("The invalid scm url connection: '" + connectionString + "'.");
115
116 throw new MojoExecutionException("Command failed. Bad Scm URL.");
117 } else {
118 getLog().info(type + " scm connection string is valid.");
119 }
120 }
121 }