View Javadoc
1   /*
2    * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and contributors.
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the GNU Lesser General Public License
6    * (LGPL) version 2.1 which accompanies this distribution, and is available at
7    * http://www.gnu.org/licenses/lgpl-2.1.html
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * Contributors:
15   *     bstefanescu, jcarsique
16   */
17  package org.nuxeo.build.maven;
18  
19  import java.util.regex.Matcher;
20  import java.util.regex.Pattern;
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.eclipse.aether.artifact.DefaultArtifact;
24  import org.eclipse.aether.graph.Dependency;
25  import org.eclipse.aether.util.artifact.JavaScopes;
26  
27  import org.nuxeo.build.maven.graph.DependencyUtils;
28  
29  /**
30   * @see org.eclipse.aether.util.artifact.ArtifactIdUtils org.eclipse.aether.artifact.DefaultArtifact
31   *      org.eclipse.aether.graph.Dependency
32   */
33  public class ArtifactDescriptor {
34  
35      public static final Pattern PATTERN = Pattern.compile("(?<groupId>[^: ]+):(?<artifactId>[^: ]+)"
36              + "(?::(?<version>[^: ]*)(?::(?<type>[^: ]*)(?::(?<classifier>[^: ]*)(?::(?<scope>[^: ]*))?)?)?)?");
37  
38      protected String groupId = null;
39  
40      protected String artifactId = null;
41  
42      protected String version = null;
43  
44      protected String type = null;
45  
46      protected String classifier = null;
47  
48      protected String scope = null;
49  
50      public ArtifactDescriptor() {
51      }
52  
53      public ArtifactDescriptor(String groupId, String artifactId, String version, String type, String classifier) {
54          this.groupId = groupId;
55          this.artifactId = artifactId;
56          this.version = version;
57          this.type = type;
58          this.classifier = classifier;
59          scope = JavaScopes.COMPILE;
60      }
61  
62      /**
63       * @param key Key for an artifact or a dependency with pattern
64       *            &lt;groupId&gt;:&lt;artifactId&gt;[:&lt;version&gt;[:&lt;type
65       *            &gt;[:&lt;classifier&gt;[:&lt;scope&gt;]]]]
66       * @see #PATTERN
67       */
68      public ArtifactDescriptor(String key) {
69          Matcher m = PATTERN.matcher(key);
70          if (!m.matches()) {
71              throw new IllegalArgumentException(String.format("Invalid key '%s', expected format is '%s'", key,
72                      "<groupId>:<artifactId>[:<version>[:<type>[:<classifier>[:<scope>]]]]"));
73          }
74          groupId = m.group("groupId");
75          artifactId = m.group("artifactId");
76          if (m.group("version") != null && !m.group("version").isEmpty()) {
77              version = m.group("version");
78          }
79          if (m.group("type") != null && !m.group("type").isEmpty()) {
80              type = m.group("type");
81          } else {
82              type = "jar";
83          }
84          if (m.group("classifier") != null && !m.group("classifier").isEmpty()) {
85              classifier = m.group("classifier");
86          }
87          if (m.group("scope") != null && !m.group("scope").isEmpty()) {
88              scope = m.group("scope");
89          } else {
90              scope = JavaScopes.COMPILE;
91          }
92      }
93  
94      public String getNodeKeyPattern() {
95          if (groupId != null) {
96              StringBuilder buf = new StringBuilder();
97              buf.append(groupId);
98              if (artifactId != null) {
99                  buf.append(':').append(artifactId);
100                 if (version != null) {
101                     buf.append(':').append(version);
102                     if (type != null) {
103                         buf.append(':').append(type);
104                     }
105                 }
106             }
107             return buf.toString();
108         }
109         return null;
110     }
111 
112     @Override
113     public String toString() {
114         StringBuilder buf = new StringBuilder();
115         buf.append(groupId).append(':').append(artifactId);
116         buf.append(':').append(version);
117         buf.append(':').append(type);
118         buf.append(':');
119         if (classifier != null) {
120             buf.append(classifier);
121         }
122         buf.append(':').append(scope);
123         return buf.toString();
124     }
125 
126     /**
127      * @return a "Maven" artifact (versus AetherArtifact)
128      * @since 1.10.2
129      */
130     public Artifact getMavenArtifact() {
131         org.eclipse.aether.artifact.Artifact aetherArtifact = getAetherArtifact();
132         return DependencyUtils.aetherToMaven(aetherArtifact, scope);
133     }
134 
135     /**
136      * Prefer use of {@link #getDependency()} if the artifact scope is needed.
137      *
138      * @since 2.0
139      */
140     public org.eclipse.aether.artifact.Artifact getAetherArtifact() {
141         return getDependency().getArtifact();
142     }
143 
144     /**
145      * @since 2.0
146      */
147     public Dependency getDependency() {
148         return new Dependency(new DefaultArtifact(groupId, artifactId, classifier, type, version), scope);
149     }
150 
151     /**
152      * @param key
153      * @return an ArtifactDescriptor corresponding to the key, or the EMPTY_DESCRIPTOR if the parsing failed.
154      * @since 2.0.4
155      */
156     public static ArtifactDescriptor parseQuietly(String key) {
157         try {
158             return new ArtifactDescriptor(key);
159         } catch (IllegalArgumentException | IllegalStateException e) {
160             // Ignore parsing issues and return an empty ArtifactDescriptor
161             return new ArtifactDescriptor();
162         }
163     }
164 
165     public String getGroupId() {
166         return groupId;
167     }
168 
169     public void setGroupId(String groupId) {
170         this.groupId = groupId;
171     }
172 
173     public String getArtifactId() {
174         return artifactId;
175     }
176 
177     public void setArtifactId(String artifactId) {
178         this.artifactId = artifactId;
179     }
180 
181     public String getVersion() {
182         return version;
183     }
184 
185     public void setVersion(String version) {
186         this.version = version;
187     }
188 
189     public String getType() {
190         return type;
191     }
192 
193     public void setType(String type) {
194         this.type = type;
195     }
196 
197     public String getClassifier() {
198         return classifier;
199     }
200 
201     public void setClassifier(String classifier) {
202         this.classifier = classifier;
203     }
204 
205     public String getScope() {
206         return scope;
207     }
208 
209     public void setScope(String scope) {
210         this.scope = scope;
211     }
212 
213     /**
214      * @since 2.0.4
215      */
216     public boolean isEmpty() {
217         return groupId == null && artifactId == null && groupId == null && scope == null && type == null
218                 && classifier == null;
219     }
220 }