1 /*
2 * Copyright (C) 2000 - 2008 Grid Systems, S.A.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17 package com.gridsystems.maven.furabuild;
18
19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.FileReader;
22 import java.util.List;
23 import java.util.Properties;
24
25 import org.apache.maven.artifact.factory.ArtifactFactory;
26 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
27 import org.apache.maven.artifact.repository.ArtifactRepository;
28 import org.apache.maven.artifact.resolver.ArtifactResolver;
29 import org.apache.maven.plugin.AbstractMojo;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.project.MavenProjectHelper;
33 import org.codehaus.plexus.util.IOUtil;
34
35 /**
36 * <p>Base class for all fura-build mojos.</p>
37 *
38 * <p>This class holds all configuration fields, in order to ensure that
39 * a single configuration can be specified for all possible goals.</p>
40 *
41 * @author Rodrigo Ruiz
42 */
43 public abstract class AbstractFuraBuildMojo extends AbstractMojo {
44
45 /**
46 * TagVersionMojo configuration: Tag file location.
47 *
48 * @parameter expression="${project.basedir}"
49 * @required
50 * @readonly
51 */
52 protected File location;
53
54 /**
55 * TagVersionMojo configuration: Version used if no tag.txt file is found.
56 *
57 * @parameter default-value="V_1_0_0"
58 */
59 protected String defaultTag;
60
61 /**
62 * TagVersionMojo configuration: List of "locations" to parse.
63 *
64 * Each entry is in the form: <tt>location;prefix;tag</tt>
65 *
66 * @parameter
67 */
68 protected List<String> locations;
69
70 /**
71 * PluginGenMojo configuration: Generated resource output directory.
72 *
73 * @parameter expression="${project.build.directory}/generated-sources"
74 * @required
75 */
76 protected File outputDirectory;
77
78 /**
79 * Reference to the Maven project.
80 *
81 * @parameter expression="${project}"
82 * @required
83 * @readonly
84 */
85 protected MavenProject project;
86
87 /**
88 * @component
89 */
90 protected MavenProjectHelper projectHelper;
91
92 /**
93 * Location of the local repository.
94 *
95 * @parameter expression="${localRepository}"
96 * @readonly
97 * @required
98 */
99 protected ArtifactRepository local;
100
101 /**
102 * Used to look up Artifacts in the remote repository.
103 *
104 * @parameter
105 * expression="${component.org.apache.maven.artifact.resolver.ArtifactResolver}"
106 * @required
107 * @readonly
108 */
109 protected ArtifactResolver resolver;
110
111 /**
112 * Used to look up Artifacts in the remote repository.
113 *
114 * @parameter
115 * expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}"
116 * @required
117 * @readonly
118 */
119 protected ArtifactFactory factory;
120
121 /**
122 * The artifact metadata source used to resolve dependencies.
123 *
124 * @component
125 */
126 protected ArtifactMetadataSource source;
127
128 /**
129 * <p>Processes a <tt>tag.txt</tt> location and puts the obtained information
130 * into <tt>props</tt>.</p>
131 *
132 * @param props Destination of all obtained information
133 * @param loc Folder containing the <tt>tag.txt</tt> file to process
134 * @param prefix Prefix used for generated properties
135 * @param defTag Default tag used if no <tt>tag.txt</tt> file is found
136 * @throws MojoExecutionException If an error occurs
137 */
138 protected final void processLocation(Properties props, File loc,
139 String prefix, String defTag) throws MojoExecutionException {
140
141 getLog().debug("tag file location: " + loc);
142 getLog().debug("tag property prefix: " + prefix);
143
144 File f = new File(loc, "tag.txt");
145
146 if (!f.isFile()) {
147 f = new File(loc.getParentFile(), "tag.txt");
148 }
149
150 VersionParser parser = new VersionParser();
151
152 if (f.isFile()) {
153 BufferedReader reader = null;
154 try {
155 reader = new BufferedReader(new FileReader(f));
156 String line = reader.readLine();
157 while (line != null) {
158 line = line.trim();
159 if (line.length() > 0 && line.charAt(0) != '#') {
160 parser.parse(props, line, prefix);
161 return;
162 }
163 }
164 } catch (Exception e) {
165 throw new MojoExecutionException("Could not read " + f.getPath(), e);
166 } finally {
167 IOUtil.close(reader);
168 }
169 } else {
170 getLog().debug("No tag.txt found.");
171 parser.parse(props, defTag, prefix);
172 }
173 }
174
175 }