View Javadoc
1   /*
2    * (C) Copyright 2011-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   *     Julien Carsique
16   *
17   */
18  
19  package org.nuxeo.build.ant.artifact;
20  
21  import java.io.File;
22  import java.io.FileFilter;
23  import java.io.FileInputStream;
24  import java.io.FileNotFoundException;
25  import java.io.IOException;
26  import java.util.ArrayList;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Properties;
30  
31  import org.apache.commons.io.filefilter.WildcardFileFilter;
32  import org.apache.tools.ant.Project;
33  import org.apache.tools.ant.types.DataType;
34  import org.apache.tools.ant.types.Resource;
35  import org.apache.tools.ant.types.ResourceCollection;
36  import org.apache.tools.ant.types.resources.FileResource;
37  import org.eclipse.aether.artifact.Artifact;
38  import org.eclipse.aether.resolution.ArtifactResolutionException;
39  import org.nuxeo.build.maven.ArtifactDescriptor;
40  import org.nuxeo.build.maven.graph.DependencyUtils;
41  
42  /**
43   * Resolve multiple files from a properties list
44   *
45   * @since 1.10.2
46   */
47  public class ResolveFiles extends DataType implements ResourceCollection {
48  
49      private Properties source;
50  
51      private String classifier;
52  
53      private List<Resource> artifacts;
54  
55      /**
56       * Set source of artifacts to resolve
57       *
58       * @param source Properties files with artifacts list
59       * @throws FileNotFoundException
60       * @throws IOException
61       */
62      public void setSource(String source) throws FileNotFoundException, IOException {
63          this.source = new Properties();
64          File sourceFile = new File(source);
65          File[] files = sourceFile.getParentFile().listFiles((FileFilter) new WildcardFileFilter(sourceFile.getName()));
66          for (File file : files) {
67              log("Loading " + file, Project.MSG_DEBUG);
68              this.source.load(new FileInputStream(file));
69          }
70      }
71  
72      /**
73       * Change classifier of all artifacts to resolve
74       *
75       * @param classifier Maven classifier
76       */
77      public void setClassifier(String classifier) {
78          this.classifier = classifier;
79      }
80  
81      @Override
82      public boolean isFilesystemOnly() {
83          return true;
84      }
85  
86      @Override
87      public Iterator<Resource> iterator() {
88          if (isReference()) {
89              return ((ResourceCollection) getCheckedRef()).iterator();
90          }
91          if (artifacts == null) {
92              artifacts = new ArrayList<>();
93              for (Iterator<?> it = source.values().iterator(); it.hasNext();) {
94                  String artifactKey = (String) it.next();
95                  try {
96                      artifacts.add(resolveFile(artifactKey));
97                  } catch (ArtifactResolutionException e) {
98                      log(e.getMessage(), Project.MSG_WARN);
99                  }
100             }
101         }
102         return artifacts.iterator();
103     }
104 
105     private FileResource resolveFile(String artifactKey) throws ArtifactResolutionException {
106         ArtifactDescriptor ad = new ArtifactDescriptor(artifactKey);
107         if (classifier != null) {
108             ad.setClassifier(classifier);
109         }
110         Artifact artifact = ad.getAetherArtifact();
111         File file;
112         if (artifact.getFile() != null) {
113             file = artifact.getFile();
114         } else {
115             if ("".equals(artifact.getVersion())) {
116                 artifact = DependencyUtils.setManagedVersion(artifact);
117             }
118             if ("".equals(artifact.getVersion())) {
119                 artifact = DependencyUtils.setNewestVersion(artifact);
120             }
121             artifact = DependencyUtils.resolve(artifact);
122             file = artifact.getFile();
123         }
124         FileResource fr = new FileResource(file);
125         fr.setBaseDir(file.getParentFile());
126         return fr;
127     }
128 
129     @Override
130     public int size() {
131         return artifacts.size();
132     }
133 
134 }