1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.release.plugin.mojos;
18
19 import java.io.File;
20 import java.util.Arrays;
21 import java.util.List;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.apache.commons.release.plugin.SharedFunctions;
25 import org.apache.maven.plugin.AbstractMojo;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.MojoFailureException;
28 import org.apache.maven.plugins.annotations.Component;
29 import org.apache.maven.plugins.annotations.LifecyclePhase;
30 import org.apache.maven.plugins.annotations.Mojo;
31 import org.apache.maven.plugins.annotations.Parameter;
32 import org.apache.maven.project.MavenProject;
33 import org.apache.maven.scm.ScmException;
34 import org.apache.maven.scm.ScmFileSet;
35 import org.apache.maven.scm.command.checkin.CheckInScmResult;
36 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
37 import org.apache.maven.scm.command.remove.RemoveScmResult;
38 import org.apache.maven.scm.manager.BasicScmManager;
39 import org.apache.maven.scm.manager.ScmManager;
40 import org.apache.maven.scm.provider.ScmProvider;
41 import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
42 import org.apache.maven.scm.provider.svn.svnexe.SvnExeScmProvider;
43 import org.apache.maven.scm.repository.ScmRepository;
44 import org.apache.maven.settings.Settings;
45 import org.apache.maven.settings.crypto.SettingsDecrypter;
46
47
48
49
50
51
52
53 @Mojo(name = "clean-staging",
54 defaultPhase = LifecyclePhase.POST_CLEAN,
55 threadSafe = true,
56 aggregator = true)
57 public final class CommonsStagingCleanupMojo extends AbstractMojo {
58
59
60
61
62
63 @Parameter(defaultValue = "${project}", required = true)
64 private MavenProject project;
65
66
67
68
69
70 @Parameter(defaultValue = "${project.build.directory}/commons-release-plugin", property = "commons.outputDirectory")
71 private File workingDirectory;
72
73
74
75
76
77 @Parameter(defaultValue = "${project.build.directory}/commons-release-plugin/scm-cleanup",
78 property = "commons.distCleanupDirectory")
79 private File distCleanupDirectory;
80
81
82
83
84
85
86 @Parameter(property = "commons.release.dryRun", defaultValue = "false")
87 private Boolean dryRun;
88
89
90
91
92
93
94 @Parameter(defaultValue = "", property = "commons.distSvnStagingUrl")
95 private String distSvnStagingUrl;
96
97
98
99
100 @Parameter(defaultValue = "false", property = "commons.release.isDistModule")
101 private Boolean isDistModule;
102
103
104
105
106
107 @Parameter(property = "commons.distServer")
108 private String distServer;
109
110
111
112
113 @Parameter(property = "user.name")
114 private String username;
115
116
117
118
119 @Parameter(property = "user.password")
120 private String password;
121
122
123
124
125 @Parameter(defaultValue = "${settings}", readonly = true, required = true)
126 private Settings settings;
127
128
129
130
131 @Component
132 private SettingsDecrypter settingsDecrypter;
133
134
135
136
137 public CommonsStagingCleanupMojo() {
138
139 }
140
141 @Override
142 public void execute() throws MojoExecutionException, MojoFailureException {
143 if (!isDistModule) {
144 getLog().info("This module is marked as a non distribution "
145 + "or assembly module, and the plugin will not run.");
146 return;
147 }
148 if (StringUtils.isEmpty(distSvnStagingUrl)) {
149 getLog().warn("commons.distSvnStagingUrl is not set, the commons-release-plugin will not run.");
150 return;
151 }
152 if (!workingDirectory.exists()) {
153 SharedFunctions.initDirectory(getLog(), workingDirectory);
154 }
155 try {
156 final ScmManager scmManager = new BasicScmManager();
157 scmManager.setScmProvider("svn", new SvnExeScmProvider());
158 final ScmRepository repository = scmManager.makeScmRepository(distSvnStagingUrl);
159 final ScmProvider provider = scmManager.getProviderByRepository(repository);
160 final SvnScmProviderRepository providerRepository = (SvnScmProviderRepository) repository
161 .getProviderRepository();
162 SharedFunctions.setAuthentication(
163 providerRepository,
164 distServer,
165 settings,
166 settingsDecrypter,
167 username,
168 password
169 );
170 getLog().info("Checking out dist from: " + distSvnStagingUrl);
171 final ScmFileSet scmFileSet = new ScmFileSet(distCleanupDirectory);
172 final CheckOutScmResult checkOutResult = provider.checkOut(repository, scmFileSet);
173 if (!checkOutResult.isSuccess()) {
174 throw new MojoExecutionException("Failed to checkout files from SCM: "
175 + checkOutResult.getProviderMessage() + " [" + checkOutResult.getCommandOutput() + "]");
176 }
177 final List<File> filesToRemove = Arrays.asList(distCleanupDirectory.listFiles());
178 if (filesToRemove.size() == 1) {
179 getLog().info("No files to delete");
180 return;
181 }
182 if (!dryRun) {
183 final ScmFileSet fileSet = new ScmFileSet(distCleanupDirectory, filesToRemove);
184 final RemoveScmResult removeScmResult = provider.remove(repository, fileSet,
185 "Cleaning up staging area");
186 if (!removeScmResult.isSuccess()) {
187 throw new MojoFailureException("Failed to remove files from SCM: "
188 + removeScmResult.getProviderMessage()
189 + " [" + removeScmResult.getCommandOutput() + "]");
190 }
191 getLog().info("Cleaning distribution area for: " + project.getArtifactId());
192 final CheckInScmResult checkInResult = provider.checkIn(
193 repository,
194 fileSet,
195 "Cleaning distribution area for: " + project.getArtifactId()
196 );
197 if (!checkInResult.isSuccess()) {
198 throw new MojoFailureException("Failed to commit files: " + removeScmResult.getProviderMessage()
199 + " [" + removeScmResult.getCommandOutput() + "]");
200 }
201 } else {
202 getLog().info("Would have attempted to delete files from: " + distSvnStagingUrl);
203 }
204 } catch (final ScmException e) {
205 throw new MojoFailureException(e.getMessage());
206 }
207
208 }
209 }