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
66 @Parameter(property = "scmCheckWorkingDirectoryUrl", defaultValue = "false")
67 private boolean scmCheckWorkingDirectoryUrl;
68
69 @Inject
70 public ValidateMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
71 super(manager, settingsDecrypter);
72 }
73
74
75
76
77 public void execute() throws MojoExecutionException {
78 super.execute();
79
80
81 try {
82 validateConnection(getConnectionUrl(), "connectionUrl");
83 } catch (NullPointerException e) {
84
85 }
86
87
88 if (scmConnection != null) {
89 validateConnection(scmConnection, "project.scm.connection");
90 }
91
92
93 if (scmDeveloperConnection != null) {
94 validateConnection(scmDeveloperConnection, "project.scm.developerConnection");
95 }
96 }
97
98 private void validateConnection(String connectionString, String type) throws MojoExecutionException {
99 if (scmCheckWorkingDirectoryUrl) {
100 System.setProperty(
101 AbstractSvnScmProvider.CURRENT_WORKING_DIRECTORY,
102 project.getFile().getParentFile().getAbsolutePath());
103 }
104 List<String> messages = getScmManager().validateScmRepository(connectionString);
105
106 if (!messages.isEmpty()) {
107 getLog().error("Validation of scm url connection (" + type + ") failed :");
108
109 Iterator<String> iter = messages.iterator();
110
111 while (iter.hasNext()) {
112 getLog().error(iter.next());
113 }
114
115 getLog().error("The invalid scm url connection: '" + connectionString + "'.");
116
117 throw new MojoExecutionException("Command failed. Bad Scm URL.");
118 } else {
119 getLog().info(type + " scm connection string is valid.");
120 }
121 }
122 }