1 | |
package org.apache.maven.plugins.scmpublish; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Collections; |
26 | |
import java.util.HashSet; |
27 | |
import java.util.List; |
28 | |
import java.util.Set; |
29 | |
|
30 | |
import org.apache.commons.io.FileUtils; |
31 | |
import org.apache.commons.io.filefilter.IOFileFilter; |
32 | |
import org.apache.maven.plugin.MojoFailureException; |
33 | |
import org.codehaus.jackson.JsonEncoding; |
34 | |
import org.codehaus.jackson.JsonGenerator; |
35 | |
import org.codehaus.jackson.JsonParser; |
36 | |
import org.codehaus.jackson.JsonProcessingException; |
37 | |
import org.codehaus.jackson.map.MappingJsonFactory; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class ScmPublishInventory |
44 | |
{ |
45 | 0 | private static class IgnoreFilter |
46 | |
implements IOFileFilter |
47 | |
{ |
48 | |
private final String ignoreFile; |
49 | |
|
50 | |
public IgnoreFilter( String ignoreFile ) |
51 | 0 | { |
52 | 0 | this.ignoreFile = ignoreFile; |
53 | 0 | } |
54 | |
|
55 | |
public boolean accept( File file ) |
56 | |
{ |
57 | 0 | return !file.getName().equals( ignoreFile ); |
58 | |
} |
59 | |
|
60 | |
public boolean accept( File dir, String name ) |
61 | |
{ |
62 | 0 | return !name.equals( ignoreFile ); |
63 | |
} |
64 | |
|
65 | |
} |
66 | |
|
67 | |
public static List<File> listInventoryFiles( File basedir, String ignoreFile ) |
68 | |
{ |
69 | 0 | List<File> inventory = new ArrayList<File>(); |
70 | 0 | inventory.addAll( FileUtils.listFiles( basedir, new IgnoreFilter( ignoreFile ), new IgnoreFilter( ignoreFile ) ) ); |
71 | 0 | Collections.sort( inventory ); |
72 | 0 | return inventory; |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public static List<File> writeInventory( List<File> inventory, File inventoryFile ) |
83 | |
throws MojoFailureException |
84 | |
{ |
85 | 0 | Set<String> paths = new HashSet<String>(); |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | 0 | for ( File f : inventory ) |
91 | |
{ |
92 | |
|
93 | 0 | if ( f.isFile() ) |
94 | |
{ |
95 | 0 | paths.add( f.getAbsolutePath() ); |
96 | |
} |
97 | |
} |
98 | |
try |
99 | |
{ |
100 | 0 | MappingJsonFactory factory = new MappingJsonFactory(); |
101 | 0 | JsonGenerator gen = factory.createJsonGenerator( inventoryFile, JsonEncoding.UTF8 ); |
102 | 0 | gen.writeObject( paths ); |
103 | 0 | gen.close(); |
104 | 0 | return inventory; |
105 | |
} |
106 | 0 | catch ( JsonProcessingException e ) |
107 | |
{ |
108 | 0 | throw new MojoFailureException( "Failed to write inventory to " + inventoryFile.getAbsolutePath(), e ); |
109 | |
} |
110 | 0 | catch ( IOException e ) |
111 | |
{ |
112 | 0 | throw new MojoFailureException( "Failed to write inventory to " + inventoryFile.getAbsolutePath(), e ); |
113 | |
} |
114 | |
} |
115 | |
|
116 | |
public static List<File> readInventory( File inventoryFile ) |
117 | |
throws MojoFailureException |
118 | |
{ |
119 | |
try |
120 | |
{ |
121 | 0 | MappingJsonFactory factory = new MappingJsonFactory(); |
122 | 0 | JsonParser parser = factory.createJsonParser( inventoryFile ); |
123 | |
@SuppressWarnings( "unchecked" ) |
124 | 0 | Set<String> storedInventory = parser.readValueAs( HashSet.class ); |
125 | 0 | List<File> inventory = new ArrayList<File>(); |
126 | 0 | for ( String p : storedInventory ) |
127 | |
{ |
128 | 0 | inventory.add( new File( p ) ); |
129 | |
} |
130 | 0 | parser.close(); |
131 | 0 | return inventory; |
132 | |
} |
133 | 0 | catch ( JsonProcessingException e ) |
134 | |
{ |
135 | 0 | throw new MojoFailureException( "Failed to write inventory to " + inventoryFile.getAbsolutePath(), e ); |
136 | |
} |
137 | 0 | catch ( IOException e ) |
138 | |
{ |
139 | 0 | throw new MojoFailureException( "Failed to write inventory to " + inventoryFile.getAbsolutePath(), e ); |
140 | |
} |
141 | |
} |
142 | |
} |