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 | 0 | 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 | 0 | if ( !StringUtils.isEmpty( include ) ) |
75 | |
{ |
76 | 0 | includes = this.parseIncludes( include ); |
77 | |
} |
78 | |
|
79 | 0 | if ( includes == null || includes.isEmpty() ) |
80 | |
{ |
81 | 0 | throw new MojoExecutionException( "Parameter manualIncludes must be specified" ); |
82 | |
} |
83 | |
|
84 | 0 | for ( String gavPattern : includes ) |
85 | |
{ |
86 | 0 | if ( StringUtils.isEmpty( gavPattern ) ) |
87 | |
{ |
88 | 0 | getLog().debug( "Skipping empty gav pattern: " + gavPattern ); |
89 | 0 | continue; |
90 | |
} |
91 | |
|
92 | 0 | String relativePath = gavToPath( gavPattern ); |
93 | 0 | if ( StringUtils.isEmpty( relativePath ) ) |
94 | |
{ |
95 | 0 | continue; |
96 | |
} |
97 | |
|
98 | 0 | File purgeDir = new File( localRepository.getBasedir(), relativePath ); |
99 | 0 | if ( purgeDir.exists() ) |
100 | |
{ |
101 | 0 | getLog().debug( "Deleting directory: " + purgeDir ); |
102 | |
try |
103 | |
{ |
104 | 0 | FileUtils.deleteDirectory( purgeDir ); |
105 | |
} |
106 | 0 | catch ( IOException e ) |
107 | |
{ |
108 | 0 | throw new MojoExecutionException( "Unable to purge directory: " + purgeDir ); |
109 | 0 | } |
110 | |
} |
111 | 0 | } |
112 | 0 | } |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
private String gavToPath( String gav ) |
121 | |
{ |
122 | 0 | if ( StringUtils.isEmpty( gav ) ) |
123 | |
{ |
124 | 0 | return null; |
125 | |
} |
126 | |
|
127 | 0 | String[] pathComponents = gav.split( ":" ); |
128 | |
|
129 | 0 | StringBuffer path = new StringBuffer( pathComponents[0].replace( '.', '/' ) ); |
130 | |
|
131 | 0 | for ( int i=1; i<pathComponents.length; ++i ) |
132 | |
{ |
133 | 0 | path.append( "/" + pathComponents[i] ); |
134 | |
} |
135 | |
|
136 | 0 | return path.toString(); |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
private List<String> parseIncludes( String include ) |
146 | |
{ |
147 | 0 | List<String> includes = new ArrayList<String>(); |
148 | |
|
149 | 0 | if ( include != null ) |
150 | |
{ |
151 | 0 | String[] elements = include.split( "," ); |
152 | 0 | includes.addAll( Arrays.asList( elements ) ); |
153 | |
} |
154 | |
|
155 | 0 | return includes; |
156 | |
} |
157 | |
|
158 | |
} |