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   *     Kohsuke Kawaguchi - initial implementation
16   *     Nuxeo - bstefanescu, jcarsique, slacoin
17   */
18  
19  package org.nuxeo.build.ant.artifact;
20  
21  import java.io.File;
22  
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.project.MavenProject;
25  import org.apache.maven.project.MavenProjectHelper;
26  import org.apache.tools.ant.BuildException;
27  import org.apache.tools.ant.Project;
28  import org.apache.tools.ant.Task;
29  import org.nuxeo.build.maven.AntBuildMojo;
30  
31  /**
32   * Attaches the artifact to Maven.
33   *
34   * @author Kohsuke Kawaguchi
35   */
36  public class AttachArtifactTask extends Task {
37  
38      private File file;
39  
40      private String classifier;
41  
42      private String type;
43  
44      /**
45       * The file to be treated as an artifact.
46       */
47      public void setFile(File file) {
48          this.file = file;
49      }
50  
51      /**
52       * Optional classifier. If left unset, the task will attach the main
53       * artifact.
54       */
55      public void setClassifier(String classifier) {
56          this.classifier = "".equals(classifier) ? null : classifier;
57      }
58  
59      /**
60       * @deprecated since 2.0 Attach now only works on the current
61       *             {@link MavenProject}
62       * @see AntBuildMojo#getProject()
63       */
64      @Deprecated
65      public void setTarget(String artifactKey) {
66          log("The target parameter is deprecated and ignored. The attach task now only applies to the current project.",
67                  Project.MSG_WARN);
68      }
69  
70      /**
71       * Artifact type. Think of it as a file extension. Optional, and if omitted,
72       * inferred from the file extension.
73       */
74      public void setType(String type) {
75          this.type = type;
76      }
77  
78      @Override
79      public void execute() throws BuildException {
80          MavenProject pom = AntBuildMojo.getInstance().getProject();
81          log("Attaching " + file + " to " + pom, Project.MSG_INFO);
82          if (type == null) {
83              type = getExtension(file.getName());
84              log("Unspecified type, using: " + type, Project.MSG_WARN);
85          }
86          Artifact pomArtifact = pom.getArtifact();
87          if (classifier != null || !type.equals(pomArtifact.getType())) {
88              AntBuildMojo.getInstance().getProjectHelper().attachArtifact(pom,
89                      type, classifier, file);
90          } else {
91              pomArtifact.setFile(file);
92              pomArtifact.setResolved(true);
93          }
94      }
95  
96      /**
97       * Guess type from the file extension whereas the default implementation of
98       * {@link MavenProjectHelper} defaults to "jar".
99       */
100     private String getExtension(String name) {
101         int idx = name.lastIndexOf('.');
102         return name.substring(idx + 1);
103     }
104 }