1 | |
package org.apache.maven.archetype.source; |
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.archetype.catalog.Archetype; |
23 | |
import org.apache.maven.archetype.catalog.ArchetypeCatalog; |
24 | |
import org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Reader; |
25 | |
import org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer; |
26 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
27 | |
import org.codehaus.plexus.util.IOUtil; |
28 | |
import org.codehaus.plexus.util.ReaderFactory; |
29 | |
import org.codehaus.plexus.util.StringUtils; |
30 | |
import org.codehaus.plexus.util.WriterFactory; |
31 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
32 | |
|
33 | |
import java.io.File; |
34 | |
import java.io.FileNotFoundException; |
35 | |
import java.io.IOException; |
36 | |
import java.io.Reader; |
37 | |
import java.io.Writer; |
38 | |
import java.util.Iterator; |
39 | |
import java.util.Properties; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 18 | public class CatalogArchetypeDataSource |
46 | |
extends AbstractLogEnabled |
47 | |
implements ArchetypeDataSource |
48 | |
{ |
49 | |
public static final String ARCHETYPE_CATALOG_PROPERTY = "file"; |
50 | |
|
51 | |
public static final String ARCHETYPE_CATALOG_FILENAME = "archetype-catalog.xml"; |
52 | |
|
53 | 1 | public static final File USER_HOME = new File( System.getProperty( "user.home" ) ); |
54 | |
|
55 | 1 | public static final File MAVEN_CONFIGURATION = new File( USER_HOME, ".m2" ); |
56 | |
|
57 | 1 | public static final File DEFAULT_ARCHETYPE_CATALOG = new File( MAVEN_CONFIGURATION, ARCHETYPE_CATALOG_FILENAME ); |
58 | |
|
59 | |
public void updateCatalog( Properties properties, Archetype archetype ) |
60 | |
throws ArchetypeDataSourceException |
61 | |
{ |
62 | 0 | String s = properties.getProperty( ARCHETYPE_CATALOG_PROPERTY ); |
63 | |
|
64 | 0 | s = StringUtils.replace( s, "${user.home}", System.getProperty( "user.home" ) ); |
65 | |
|
66 | 0 | getLogger().debug( "Using catalog " + s ); |
67 | |
|
68 | 0 | File catalogFile = new File( s ); |
69 | |
|
70 | |
ArchetypeCatalog catalog; |
71 | 0 | if ( catalogFile.exists() ) |
72 | |
{ |
73 | |
try |
74 | |
{ |
75 | 0 | getLogger().debug( "Reading the catalog " + catalogFile ); |
76 | 0 | catalog = readCatalog( ReaderFactory.newXmlReader( catalogFile ) ); |
77 | |
} |
78 | 0 | catch ( FileNotFoundException ex ) |
79 | |
{ |
80 | 0 | getLogger().debug( "Catalog file don't exist" ); |
81 | 0 | catalog = new ArchetypeCatalog(); |
82 | |
} |
83 | 0 | catch ( IOException e ) |
84 | |
{ |
85 | 0 | throw new ArchetypeDataSourceException( "Error reading archetype catalog.", e ); |
86 | 0 | } |
87 | |
} |
88 | |
else |
89 | |
{ |
90 | 0 | getLogger().debug( "Catalog file don't exist" ); |
91 | 0 | catalog = new ArchetypeCatalog(); |
92 | |
} |
93 | |
|
94 | 0 | Iterator<Archetype> archetypes = catalog.getArchetypes().iterator(); |
95 | 0 | boolean found = false; |
96 | 0 | Archetype newArchetype = archetype; |
97 | 0 | while ( !found && archetypes.hasNext() ) |
98 | |
{ |
99 | 0 | Archetype a = (Archetype) archetypes.next(); |
100 | 0 | if ( a.getGroupId().equals( archetype.getGroupId() ) |
101 | |
&& a.getArtifactId().equals( archetype.getArtifactId() ) ) |
102 | |
{ |
103 | 0 | newArchetype = a; |
104 | 0 | found = true; |
105 | |
} |
106 | 0 | } |
107 | 0 | if ( !found ) |
108 | |
{ |
109 | 0 | catalog.addArchetype( newArchetype ); |
110 | |
} |
111 | |
|
112 | 0 | newArchetype.setVersion( archetype.getVersion() ); |
113 | 0 | newArchetype.setRepository( archetype.getRepository() ); |
114 | 0 | newArchetype.setDescription( archetype.getDescription() ); |
115 | 0 | newArchetype.setProperties( archetype.getProperties() ); |
116 | 0 | newArchetype.setGoals( archetype.getGoals() ); |
117 | |
|
118 | 0 | writeLocalCatalog( catalog, catalogFile ); |
119 | 0 | } |
120 | |
|
121 | |
public ArchetypeCatalog getArchetypeCatalog( Properties properties ) |
122 | |
throws ArchetypeDataSourceException |
123 | |
{ |
124 | 2 | String s = properties.getProperty( ARCHETYPE_CATALOG_PROPERTY ); |
125 | |
|
126 | 2 | s = StringUtils.replace( s, "${user.home}", System.getProperty( "user.home" ) ); |
127 | |
|
128 | 2 | File catalogFile = new File( s ); |
129 | 2 | if ( catalogFile.exists() && catalogFile.isDirectory() ) |
130 | |
{ |
131 | 2 | catalogFile = new File( catalogFile, ARCHETYPE_CATALOG_FILENAME ); |
132 | |
} |
133 | 2 | getLogger().debug( "Using catalog " + catalogFile ); |
134 | |
|
135 | 2 | if ( catalogFile.exists() ) |
136 | |
{ |
137 | |
try |
138 | |
{ |
139 | 2 | return readCatalog( ReaderFactory.newXmlReader( catalogFile ) ); |
140 | |
} |
141 | 0 | catch ( FileNotFoundException e ) |
142 | |
{ |
143 | 0 | throw new ArchetypeDataSourceException( "The specific archetype catalog does not exist.", e ); |
144 | |
} |
145 | 0 | catch ( IOException e ) |
146 | |
{ |
147 | 0 | throw new ArchetypeDataSourceException( "Error reading archetype catalog.", e ); |
148 | |
} |
149 | |
} |
150 | |
else |
151 | |
{ |
152 | 0 | return new ArchetypeCatalog(); |
153 | |
} |
154 | |
} |
155 | |
|
156 | |
protected void writeLocalCatalog( ArchetypeCatalog catalog, File catalogFile ) |
157 | |
throws ArchetypeDataSourceException |
158 | |
{ |
159 | 0 | Writer writer = null; |
160 | |
try |
161 | |
{ |
162 | 0 | writer = WriterFactory.newXmlWriter( catalogFile ); |
163 | |
|
164 | 0 | ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer(); |
165 | |
|
166 | 0 | catalogWriter.write( writer, catalog ); |
167 | |
} |
168 | 0 | catch ( IOException e ) |
169 | |
{ |
170 | 0 | throw new ArchetypeDataSourceException( "Error writing archetype catalog.", e ); |
171 | |
} |
172 | |
finally |
173 | |
{ |
174 | 0 | IOUtil.close( writer ); |
175 | 0 | } |
176 | 0 | } |
177 | |
|
178 | |
protected ArchetypeCatalog readCatalog( Reader reader ) |
179 | |
throws ArchetypeDataSourceException |
180 | |
{ |
181 | |
try |
182 | |
{ |
183 | 3 | ArchetypeCatalogXpp3Reader catalogReader = new ArchetypeCatalogXpp3Reader(); |
184 | |
|
185 | 3 | return catalogReader.read( reader ); |
186 | |
} |
187 | 0 | catch ( IOException e ) |
188 | |
{ |
189 | 0 | throw new ArchetypeDataSourceException( "Error reading archetype catalog.", e ); |
190 | |
} |
191 | 0 | catch ( XmlPullParserException e ) |
192 | |
{ |
193 | 0 | throw new ArchetypeDataSourceException( "Error parsing archetype catalog.", e ); |
194 | |
} |
195 | |
finally |
196 | |
{ |
197 | 3 | IOUtil.close( reader ); |
198 | |
} |
199 | |
} |
200 | |
} |