View Javadoc
1   /*
2    * (C) Copyright 2009-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   *     Sun Seng David TAN, jcarsique
16   */
17  package org.nuxeo.build.ant.artifact;
18  
19  import java.io.IOException;
20  
21  import org.apache.tools.ant.BuildException;
22  import org.apache.tools.ant.taskdefs.Sequential;
23  import org.eclipse.aether.artifact.Artifact;
24  
25  /**
26   * Iterate through an artifact set
27   *
28   * Usage:
29   * {@code <artifact:foreach setref="bundles" artifactJarPathProperty="path" >
30   * (...) </artifact:foreach>}
31   *
32   */
33  public class ArtifactForeach extends Sequential {
34  
35      public String property;
36  
37      public ArtifactSet artifactSet;
38  
39      public String getProperty() {
40          return property;
41      }
42  
43      public void setProperty(String property) {
44          this.property = property;
45      }
46  
47      public ArtifactSet getArtifactSet() {
48          return artifactSet;
49      }
50  
51      public void setSetref(String setref) {
52          artifactSet = (ArtifactSet) getProject().getReference(setref);
53  
54      }
55  
56      @Override
57      public void execute() throws BuildException {
58          for (Artifact artifact : artifactSet.getArtifacts()) {
59              String canonicalPath = null;
60              try {
61                  canonicalPath = artifact.getFile().getCanonicalPath();
62              } catch (IOException e) {
63                  throw new BuildException(
64                          "Failed to artifact file canonical path for "
65                                  + artifact, e);
66              }
67              getProject().setProperty(property + ".file.path", canonicalPath);
68              getProject().setProperty(property + ".archetypeId",
69                      artifact.getArtifactId());
70              getProject().setProperty(property + ".groupId",
71                      artifact.getGroupId());
72              getProject().setProperty(property + ".version",
73                      artifact.getBaseVersion());
74              try {
75                  super.execute();
76              } catch (BuildException e) {
77                  throw new BuildException(
78                          "Couldn't execute the task for artifact " + artifact, e);
79              }
80          }
81  
82      }
83  }