View Javadoc
1   /*
2    * (C) Copyright 2006-2014 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.ant.artifact;
18  
19  import java.io.File;
20  
21  import org.apache.tools.ant.BuildException;
22  import org.apache.tools.ant.Project;
23  import org.apache.tools.ant.types.resources.FileResource;
24  import org.eclipse.aether.artifact.Artifact;
25  import org.eclipse.aether.resolution.ArtifactResolutionException;
26  import org.nuxeo.build.maven.ArtifactDescriptor;
27  import org.nuxeo.build.maven.graph.DependencyUtils;
28  
29  /**
30   * TODO NXBT-258
31   */
32  public class ResolveFile extends FileResource {
33  
34      protected String key;
35  
36      protected String classifier;
37  
38      private File file = null;
39  
40      public void setKey(String pattern) {
41          int p = pattern.lastIndexOf(';');
42          if (p > -1) {
43              key = pattern.substring(0, p);
44              classifier = pattern.substring(p + 1);
45          } else {
46              key = pattern;
47          }
48      }
49  
50      /**
51       * @deprecated since 1.8; put classifier in the key ("groupId:artifactId:version:type:classifier:scope")
52       * @param classifier
53       */
54      @Deprecated
55      public void setClassifier(String classifier) {
56          log("The classifier parameter is deprecated, put it in the key.", Project.MSG_WARN);
57          this.classifier = classifier;
58      }
59  
60      protected File resolveFile() {
61          ArtifactDescriptor ad = new ArtifactDescriptor(key);
62          // Sync classifier set from key or from setClassifier()
63          if (ad.getClassifier() != null) {
64              classifier = ad.getClassifier();
65          } else if (classifier != null) {
66              ad.setClassifier(classifier);
67          }
68          try {
69              if (file != null) {
70                  return file;
71              }
72              Artifact artifact = ad.getAetherArtifact();
73              if (artifact.getFile() != null) {
74                  file = artifact.getFile();
75                  return file;
76              }
77              if ("".equals(artifact.getVersion())) {
78                  artifact = DependencyUtils.setManagedVersion(artifact);
79              }
80              if ("".equals(artifact.getVersion())) {
81                  artifact = DependencyUtils.setNewestVersion(artifact);
82              }
83              artifact = DependencyUtils.resolve(artifact);
84              file = artifact.getFile();
85              return file;
86          } catch (ArtifactResolutionException e) {
87              throw new BuildException(String.format("Cannot resolve file with key '%s'", ad), e);
88          }
89      }
90  
91      @Override
92      public File getFile() {
93          if (isReference()) {
94              return ((FileResource) getCheckedRef()).getFile();
95          }
96          return resolveFile();
97      }
98  
99      @Override
100     public File getBaseDir() {
101         return isReference() ? ((FileResource) getCheckedRef()).getBaseDir() : getFile().getParentFile();
102     }
103 
104 }