1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.extension.internal.maven

File MavenUtils.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

40
61
8
1
258
125
30
0.49
7.62
8
3.75

Classes

Class Line # Actions
MavenUtils 37 61 0% 30 9
0.917431291.7%
 

Contributing tests

This file is covered by 19 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.extension.internal.maven;
21   
22    import java.util.Map;
23    import java.util.regex.Pattern;
24   
25    import org.apache.commons.lang3.StringUtils;
26    import org.apache.maven.model.Model;
27    import org.apache.maven.model.Parent;
28    import org.xwiki.extension.DefaultExtensionScmConnection;
29    import org.xwiki.extension.ExtensionScmConnection;
30   
31    /**
32    * Various Maven related helpers.
33    *
34    * @version $Id: ebd0f2b369fedfe378611e835ccbd6e93d0dda4c $
35    * @since 7.0M1
36    */
 
37    public class MavenUtils
38    {
39    public static final String PKEY_MAVEN_MODEL = "maven.Model";
40   
41    public static final String JAR_EXTENSION = "jar";
42   
43    public static final String JAVA_LANGUAGE = "java";
44   
45    /**
46    * The package containing maven informations in a jar file.
47    */
48    public static final String MAVENPACKAGE = "META-INF.maven";
49   
50    /**
51    * SNAPSHOT suffix in versions.
52    */
53    public static final String SNAPSHOTSUFFIX = "-SNAPSHOT";
54   
55    /**
56    * Used to parse extension id into maven informations.
57    */
58    public static final Pattern PARSER_ID = Pattern.compile("([^: ]+):([^: ]+)(:([^: ]+))?");
59   
60    /**
61    * MANIFEST.MF attribute containing extension identifier.
62    */
63    public static final String MF_EXTENSION_ID = "XWiki-Extension-Id";
64   
65    /**
66    * Unknown.
67    */
68    public static final String UNKNOWN = "unknown";
69   
70    /**
71    * Parse a Maven scm URL to generate a {@link ExtensionScmConnection}.
72    *
73    * @param connectionURL the connection URL
74    * @return the {@link ExtensionScmConnection}
75    */
 
76  31264 toggle public static ExtensionScmConnection toExtensionScmConnection(String connectionURL)
77    {
78  31264 if (connectionURL == null) {
79  2773 return null;
80    }
81   
82  28491 String path = connectionURL;
83   
84  28491 if (path.startsWith("scm:")) {
85  27916 path = path.substring("scm:".length());
86    }
87   
88  28491 String system = "git";
89  28491 int index = path.indexOf(':');
90  28491 if (index >= 0) {
91  28458 if (index != 0) {
92  28458 system = path.substring(0, index);
93    }
94  28458 path = path.substring(index + 1);
95    }
96   
97  28491 return new DefaultExtensionScmConnection(system, path);
98    }
99   
100    /**
101    * Create a extension identifier from Maven artifact identifier elements.
102    *
103    * @param groupId the group id
104    * @param artifactId the artifact id
105    * @param classifier the classifier
106    * @return the extension identifier
107    */
 
108  722474 toggle public static String toExtensionId(String groupId, String artifactId, String classifier)
109    {
110  722474 StringBuilder builder = new StringBuilder();
111   
112  722474 builder.append(groupId);
113  722474 builder.append(':');
114  722474 builder.append(artifactId);
115  722474 if (StringUtils.isNotEmpty(classifier)) {
116  6801 builder.append(':');
117  6801 builder.append(classifier);
118    }
119   
120  722474 return builder.toString();
121    }
122   
123    /**
124    * Get the extension type from maven packaging.
125    *
126    * @param packaging the maven packaging
127    * @return the extension type
128    */
 
129  26406 toggle public static String packagingToType(String packaging)
130    {
131    // support bundle packaging
132  26406 if (packaging.equals("bundle")) {
133  1855 return "jar";
134    }
135   
136  24551 return packaging;
137    }
138   
139    /**
140    * @param mavenModel the Maven Model instance
141    * @return the resolved version
142    * @since 8.1M1
143    */
 
144  35305 toggle public static String resolveVersion(Model mavenModel)
145    {
146  35305 return resolveVersion(mavenModel.getVersion(), mavenModel, false);
147    }
148   
149    /**
150    * @param modelVersion the current String representing the version to resolve
151    * @param mavenModel the Maven Model instance
152    * @param dependency indicate if it's a dependency version
153    * @return the resolved version
154    * @since 8.1M1
155    */
 
156  769671 toggle public static String resolveVersion(String modelVersion, Model mavenModel, boolean dependency)
157    {
158  769671 String version = modelVersion;
159   
160    // TODO: download parents and resolve pom.xml properties using standard tools ? could be pretty expensive for
161    // the init
162  769671 if (version == null) {
163  17223 if (!dependency) {
164  7870 Parent parent = mavenModel.getParent();
165   
166  7870 if (parent != null) {
167  7870 version = parent.getVersion();
168    }
169    }
170  752448 } else if (version.startsWith("$")) {
171  18358 String propertyName = version.substring(2, version.length() - 1);
172   
173  18358 if (propertyName.equals("project.version") || propertyName.equals("pom.version")
174    || propertyName.equals("version")) {
175  11947 version = resolveVersion(mavenModel.getVersion(), mavenModel, false);
176    } else {
177  6411 String value = mavenModel.getProperties().getProperty(propertyName);
178  6411 if (value != null) {
179  1072 version = value;
180    }
181    }
182    }
183   
184  769671 if (version == null) {
185  9353 version = UNKNOWN;
186    }
187   
188  769671 return version;
189    }
190   
191    /**
192    * @param mavenModel the Maven Model instance
193    * @return the resolved group id
194    * @since 8.1M1
195    */
 
196  35305 toggle public static String resolveGroupId(Model mavenModel)
197    {
198  35305 return resolveGroupId(mavenModel.getGroupId(), mavenModel, false);
199    }
200   
201    /**
202    * @param modelVersion the current String representing the group id to resolve
203    * @param mavenModel the Maven Model instance
204    * @param dependency indicate if it's a dependency group id
205    * @return the resolved group id
206    * @since 8.1M1
207    */
 
208  757724 toggle public static String resolveGroupId(String modelGroupId, Model mavenModel, boolean dependency)
209    {
210  757724 String groupId = modelGroupId;
211   
212    // TODO: download parents and resolve pom.xml properties using standard tools ? could be pretty expensive for
213    // the init
214  757724 if (groupId == null) {
215  7860 if (!dependency) {
216  7860 Parent parent = mavenModel.getParent();
217   
218  7860 if (parent != null) {
219  7860 groupId = parent.getGroupId();
220    }
221    }
222  749864 } else if (groupId.startsWith("$")) {
223  210 String propertyName = groupId.substring(2, groupId.length() - 1);
224   
225  210 String value = mavenModel.getProperties().getProperty(propertyName);
226  210 if (value != null) {
227  0 groupId = value;
228    }
229    }
230   
231  757724 if (groupId == null) {
232  0 groupId = UNKNOWN;
233    }
234   
235  757724 return groupId;
236    }
237   
238    /**
239    * @param model the Maven Model instance to update
240    */
241    // TODO: use standard Maven tools for that
 
242  10899 toggle public static void resolveVariables(Model model)
243    {
244    // Resolve version
245  10899 model.setVersion(resolveVersion(model));
246   
247    // Resolve groupid
248  10899 model.setGroupId(resolveGroupId(model));
249   
250    // Resolve properties
251  10899 for (Map.Entry<Object, Object> entry : model.getProperties().entrySet()) {
252  24005 if (entry.getValue() instanceof String) {
253  24005 String value = (String) entry.getValue();
254  24005 entry.setValue(value.replace("${project.version}", model.getVersion()));
255    }
256    }
257    }
258    }