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.project.MavenProject;
33
34
35
36
37
38
39
40
41
42
43
44 public final class ObrInstall extends AbstractMojo
45 {
46
47
48
49
50
51 private String obrRepository;
52
53
54
55
56
57
58 private List supportedProjectTypes = Arrays.asList( new String[]
59 { "jar", "bundle" } );
60
61
62
63
64
65
66
67
68 private ArtifactRepository localRepository;
69
70
71
72
73
74
75
76
77 private MavenProject project;
78
79
80
81
82
83
84 private List attachedArtifacts;
85
86
87
88
89 private Artifact m_sourceArtifact;
90
91
92
93
94 private Artifact m_docArtifact;
95
96
97 public void execute()
98 {
99 String projectType = project.getPackaging();
100
101
102 if ( !supportedProjectTypes.contains( projectType ) )
103 {
104 getLog().warn(
105 "Ignoring project type " + projectType + " - supportedProjectTypes = " + supportedProjectTypes );
106 return;
107 }
108 else if ( "NONE".equalsIgnoreCase( obrRepository ) || "false".equalsIgnoreCase( obrRepository ) )
109 {
110 getLog().info( "Local OBR update disabled (enable with -DobrRepository)" );
111 return;
112 }
113
114
115 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
116 {
117 Artifact artifact = ( Artifact ) i.next();
118 if ( "sources".equals( artifact.getClassifier() ) )
119 {
120 m_sourceArtifact = artifact;
121 }
122 else if ( "javadoc".equals( artifact.getClassifier() ) )
123 {
124 m_docArtifact = artifact;
125 }
126 }
127
128 Log log = getLog();
129 ObrUpdate update;
130
131 try
132 {
133 String mavenRepository = localRepository.getBasedir();
134
135 URI repositoryXml = ObrUtils.findRepositoryXml( mavenRepository, obrRepository );
136 URI obrXmlFile = ObrUtils.findObrXml( project );
137
138 Config userConfig = new Config();
139
140 update = new ObrUpdate( repositoryXml, obrXmlFile, project, mavenRepository, userConfig, log );
141 update.parseRepositoryXml();
142
143 updateLocalBundleMetadata( project.getArtifact(), update );
144 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
145 {
146 updateLocalBundleMetadata( ( Artifact ) i.next(), update );
147 }
148
149 update.writeRepositoryXml();
150 }
151 catch ( Exception e )
152 {
153 log.warn( "Exception while updating local OBR: " + e.getLocalizedMessage(), e );
154 }
155 }
156
157
158 private void updateLocalBundleMetadata( Artifact artifact, ObrUpdate update ) throws MojoExecutionException
159 {
160 if ( !supportedProjectTypes.contains( artifact.getType() ) )
161 {
162 return;
163 }
164 else if ( null == artifact.getFile() || artifact.getFile().isDirectory() )
165 {
166 getLog().error( "No artifact found, try \"mvn install bundle:install\"" );
167 return;
168 }
169
170 URI bundleJar = ObrUtils.getArtifactURI( localRepository, artifact );
171
172 URI sourceJar = null;
173 if ( null != m_sourceArtifact )
174 {
175 sourceJar = ObrUtils.getArtifactURI( localRepository, m_sourceArtifact );
176 }
177
178 URI docJar = null;
179 if ( null != m_docArtifact )
180 {
181 docJar = ObrUtils.getArtifactURI( localRepository, m_docArtifact );
182 }
183
184 update.updateRepository( bundleJar, sourceJar, docJar );
185 }
186 }