1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.felix.obrplugin;
20
21
22 import java.net.URI;
23 import java.util.Arrays;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.repository.ArtifactRepository;
29 import org.apache.maven.plugin.AbstractMojo;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.plugin.logging.Log;
32 import org.apache.maven.plugins.annotations.LifecyclePhase;
33 import org.apache.maven.plugins.annotations.Mojo;
34 import org.apache.maven.plugins.annotations.Parameter;
35 import org.apache.maven.project.MavenProject;
36
37
38
39
40
41
42
43 @Mojo( name = "install", threadSafe = true, defaultPhase = LifecyclePhase.INSTALL )
44 public final class ObrInstall extends AbstractMojo
45 {
46
47
48
49 @Parameter( property = "obrRepository" )
50 private String obrRepository;
51
52
53
54
55 @Parameter
56 private List supportedProjectTypes = Arrays.asList( new String[]
57 { "jar", "bundle" } );
58
59
60
61
62 @Parameter( defaultValue = "${localRepository}", readonly = true, required = true )
63 private ArtifactRepository localRepository;
64
65
66
67
68 @Parameter( defaultValue = "${project}", readonly = true, required = true )
69 private MavenProject project;
70
71 @Parameter( defaultValue = "${project.attachedArtifacts}", readonly = true, required = true )
72 private List attachedArtifacts;
73
74
75
76
77 private Artifact m_sourceArtifact;
78
79
80
81
82 private Artifact m_docArtifact;
83
84
85 public void execute()
86 {
87 String projectType = project.getPackaging();
88
89
90 if ( !supportedProjectTypes.contains( projectType ) )
91 {
92 getLog().warn(
93 "Ignoring project type " + projectType + " - supportedProjectTypes = " + supportedProjectTypes );
94 return;
95 }
96 else if ( "NONE".equalsIgnoreCase( obrRepository ) || "false".equalsIgnoreCase( obrRepository ) )
97 {
98 getLog().info( "Local OBR update disabled (enable with -DobrRepository)" );
99 return;
100 }
101
102
103 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
104 {
105 Artifact artifact = ( Artifact ) i.next();
106 if ( "sources".equals( artifact.getClassifier() ) )
107 {
108 m_sourceArtifact = artifact;
109 }
110 else if ( "javadoc".equals( artifact.getClassifier() ) )
111 {
112 m_docArtifact = artifact;
113 }
114 }
115
116 Log log = getLog();
117 ObrUpdate update;
118
119 try
120 {
121 String mavenRepository = localRepository.getBasedir();
122
123 URI repositoryXml = ObrUtils.findRepositoryXml( mavenRepository, obrRepository );
124 URI obrXmlFile = ObrUtils.findObrXml( project );
125
126 Config userConfig = new Config();
127
128 update = new ObrUpdate( repositoryXml, obrXmlFile, project, mavenRepository, userConfig, log );
129 synchronized ( ObrUpdate.class )
130 {
131 update.parseRepositoryXml();
132
133 updateLocalBundleMetadata( project.getArtifact(), update );
134 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
135 {
136 updateLocalBundleMetadata( ( Artifact ) i.next(), update );
137 }
138
139 update.writeRepositoryXml();
140 }
141 }
142 catch ( Exception e )
143 {
144 log.warn( "Exception while updating local OBR: " + e.getLocalizedMessage(), e );
145 }
146 }
147
148
149 private void updateLocalBundleMetadata( Artifact artifact, ObrUpdate update ) throws MojoExecutionException
150 {
151 if ( !supportedProjectTypes.contains( artifact.getType() ) )
152 {
153 return;
154 }
155 else if ( null == artifact.getFile() || artifact.getFile().isDirectory() )
156 {
157 getLog().error( "No artifact found, try \"mvn install bundle:install\"" );
158 return;
159 }
160
161 URI bundleJar = ObrUtils.getArtifactURI( localRepository, artifact );
162
163 URI sourceJar = null;
164 if ( null != m_sourceArtifact )
165 {
166 sourceJar = ObrUtils.getArtifactURI( localRepository, m_sourceArtifact );
167 }
168
169 URI docJar = null;
170 if ( null != m_docArtifact )
171 {
172 docJar = ObrUtils.getArtifactURI( localRepository, m_docArtifact );
173 }
174
175 update.updateRepository( bundleJar, sourceJar, docJar );
176 }
177 }