1 package org.apache.maven.plugin.dependency;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.repository.ArtifactRepository;
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.plugins.annotations.Mojo;
27 import org.apache.maven.plugins.annotations.Parameter;
28 import org.codehaus.plexus.util.FileUtils;
29 import org.codehaus.plexus.util.StringUtils;
30
31 import java.io.File;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36
37
38
39
40
41
42
43 @Mojo( name = "manual-purge-local-repository", threadSafe = true, requiresProject = false )
44 public class ManualPurgeLocalRepositoryMojo
45 extends AbstractMojo
46 {
47
48
49
50
51 @Parameter( defaultValue = "${localRepository}", readonly = true, required = true )
52 private ArtifactRepository localRepository;
53
54
55
56
57
58
59 @Parameter
60 private List<String> includes;
61
62
63
64
65
66
67 @Parameter( property = "include" )
68 private String include;
69
70
71 public void execute()
72 throws MojoExecutionException, MojoFailureException
73 {
74 if ( !StringUtils.isEmpty( include ) )
75 {
76 includes = this.parseIncludes( include );
77 }
78
79 if ( includes == null || includes.isEmpty() )
80 {
81 throw new MojoExecutionException( "Parameter manualIncludes must be specified" );
82 }
83
84 for ( String gavPattern : includes )
85 {
86 if ( StringUtils.isEmpty( gavPattern ) )
87 {
88 getLog().debug( "Skipping empty gav pattern: " + gavPattern );
89 continue;
90 }
91
92 String relativePath = gavToPath( gavPattern );
93 if ( StringUtils.isEmpty( relativePath ) )
94 {
95 continue;
96 }
97
98 File purgeDir = new File( localRepository.getBasedir(), relativePath );
99 if ( purgeDir.exists() )
100 {
101 getLog().debug( "Deleting directory: " + purgeDir );
102 try
103 {
104 FileUtils.deleteDirectory( purgeDir );
105 }
106 catch ( IOException e )
107 {
108 throw new MojoExecutionException( "Unable to purge directory: " + purgeDir );
109 }
110 }
111 }
112 }
113
114
115
116
117
118
119
120 private String gavToPath( String gav )
121 {
122 if ( StringUtils.isEmpty( gav ) )
123 {
124 return null;
125 }
126
127 String[] pathComponents = gav.split( ":" );
128
129 StringBuffer path = new StringBuffer( pathComponents[0].replace( '.', '/' ) );
130
131 for ( int i=1; i<pathComponents.length; ++i )
132 {
133 path.append( "/" + pathComponents[i] );
134 }
135
136 return path.toString();
137 }
138
139
140
141
142
143
144
145 private List<String> parseIncludes( String include )
146 {
147 List<String> includes = new ArrayList<String>();
148
149 if ( include != null )
150 {
151 String[] elements = include.split( "," );
152 includes.addAll( Arrays.asList( elements ) );
153 }
154
155 return includes;
156 }
157
158 }