View Javadoc
1   package org.apache.maven.plugin.coreit;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.deployer.ArtifactDeployer;
24  import org.apache.maven.artifact.factory.ArtifactFactory;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
27  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
28  import org.apache.maven.plugin.AbstractMojo;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.MojoFailureException;
31  
32  import java.io.File;
33  
34  /**
35   * Deploys a user-supplied file to some repository. This mimics part of the Maven Deploy Plugin.
36   *
37   * @goal deploy-file
38   * @requiresProject false
39   *
40   * @author Benjamin Bentmann
41   *
42   */
43  public class DeployFileMojo
44      extends AbstractMojo
45  {
46  
47      /**
48       * The file of the artifact to deploy.
49       *
50       * @parameter property="file"
51       */
52      private File file;
53  
54      /**
55       * The group id of the artifact.
56       *
57       * @parameter property="groupId"
58       */
59      private String groupId;
60  
61      /**
62       * The artifact id of the artifact.
63       *
64       * @parameter property="artifactId"
65       */
66      private String artifactId;
67  
68      /**
69       * The version of the artifact.
70       *
71       * @parameter property="version"
72       */
73      private String version;
74  
75      /**
76       * The URL of the repository to deploy to.
77       *
78       * @parameter property="repositoryUrl"
79       */
80      private String repositoryUrl;
81  
82      /**
83       * The ID of the repository to deploy to.
84       *
85       * @parameter property="repositoryId"
86       */
87      private String repositoryId;
88  
89      /**
90       * The repository factory.
91       *
92       * @component
93       */
94      private ArtifactRepositoryFactory repositoryFactory;
95  
96      /**
97       * The repository layout.
98       *
99       * @component roleHint="default"
100      */
101     private ArtifactRepositoryLayout repositoryLayout;
102 
103     /**
104      * The artifact factory.
105      *
106      * @component
107      */
108     private ArtifactFactory artifactFactory;
109 
110     /**
111      * The artifact deployer.
112      *
113      * @component
114      */
115     private ArtifactDeployer deployer;
116 
117     /**
118      * The local repository.
119      *
120      * @parameter default-value="${localRepository}"
121      * @readonly
122      * @required
123      */
124     private ArtifactRepository localRepository;
125 
126     /**
127      * Runs this mojo.
128      *
129      * @throws MojoExecutionException If any artifact could not be deployed.
130      */
131     public void execute()
132         throws MojoExecutionException, MojoFailureException
133     {
134         getLog().info( "[MAVEN-CORE-IT-LOG] Deploying artifacts" );
135 
136         try
137         {
138             ArtifactRepository repository =
139                 repositoryFactory.createDeploymentArtifactRepository( repositoryId, repositoryUrl, repositoryLayout,
140                                                                       true );
141 
142             Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, "jar" );
143 
144             deployer.deploy( file, artifact, repository, localRepository );
145         }
146         catch ( Exception e )
147         {
148             throw new MojoExecutionException( "Failed to deploy artifacts: " + e.getMessage(), e );
149         }
150     }
151 
152 }