1 package org.apache.maven.plugin.install;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.installer.ArtifactInstallationException;
24 import org.apache.maven.artifact.metadata.ArtifactMetadata;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugins.annotations.LifecyclePhase;
27 import org.apache.maven.plugins.annotations.Mojo;
28 import org.apache.maven.plugins.annotations.Parameter;
29 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
30
31 import java.io.File;
32 import java.util.Collection;
33 import java.util.Iterator;
34 import java.util.LinkedHashSet;
35 import java.util.List;
36
37
38
39
40
41
42
43 @Mojo( name = "install", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true )
44 public class InstallMojo
45 extends AbstractInstallMojo
46 {
47
48
49 @Parameter( defaultValue = "${project.packaging}", required = true, readonly = true )
50 protected String packaging;
51
52
53
54 @Parameter( defaultValue = "${project.file}", required = true, readonly = true )
55 private File pomFile;
56
57
58
59
60
61
62
63 @Parameter( property = "maven.install.skip", defaultValue = "false", required = true )
64 private boolean skip;
65
66
67
68 @Parameter( defaultValue = "${project.artifact}", required = true, readonly = true )
69 private Artifact artifact;
70
71
72
73 @Parameter( defaultValue = "${project.attachedArtifacts}", required = true, readonly = true )
74 private List attachedArtifacts;
75
76 public void execute()
77 throws MojoExecutionException
78 {
79 if ( skip )
80 {
81 getLog().info( "Skipping artifact installation" );
82 return;
83 }
84
85
86 boolean isPomArtifact = "pom".equals( packaging );
87
88 ArtifactMetadata metadata = null;
89
90 if ( updateReleaseInfo )
91 {
92 artifact.setRelease( true );
93 }
94
95 try
96 {
97 Collection metadataFiles = new LinkedHashSet();
98
99 if ( isPomArtifact )
100 {
101 installer.install( pomFile, artifact, localRepository );
102 installChecksums( artifact, metadataFiles );
103 }
104 else
105 {
106 metadata = new ProjectArtifactMetadata( artifact, pomFile );
107 artifact.addMetadata( metadata );
108
109 File file = artifact.getFile();
110
111
112
113 if ( file != null && file.isFile() )
114 {
115 installer.install( file, artifact, localRepository );
116 installChecksums( artifact, metadataFiles );
117 }
118 else if ( !attachedArtifacts.isEmpty() )
119 {
120 getLog().info( "No primary artifact to install, installing attached artifacts instead." );
121
122 Artifact pomArtifact =
123 artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
124 artifact.getBaseVersion() );
125 pomArtifact.setFile( pomFile );
126 if ( updateReleaseInfo )
127 {
128 pomArtifact.setRelease( true );
129 }
130
131 installer.install( pomFile, pomArtifact, localRepository );
132 installChecksums( pomArtifact, metadataFiles );
133 }
134 else
135 {
136 throw new MojoExecutionException(
137 "The packaging for this project did not assign a file to the build artifact" );
138 }
139 }
140
141 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
142 {
143 Artifact attached = (Artifact) i.next();
144
145 installer.install( attached.getFile(), attached, localRepository );
146 installChecksums( attached, metadataFiles );
147 }
148
149 installChecksums( metadataFiles );
150 }
151 catch ( ArtifactInstallationException e )
152 {
153 throw new MojoExecutionException( e.getMessage(), e );
154 }
155 }
156
157 public void setSkip( boolean skip )
158 {
159 this.skip = skip;
160 }
161 }