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 synchronized ( ObrUpdate.class )
142 {
143 update.parseRepositoryXml();
144
145 updateLocalBundleMetadata( project.getArtifact(), update );
146 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
147 {
148 updateLocalBundleMetadata( ( Artifact ) i.next(), update );
149 }
150
151 update.writeRepositoryXml();
152 }
153 }
154 catch ( Exception e )
155 {
156 log.warn( "Exception while updating local OBR: " + e.getLocalizedMessage(), e );
157 }
158 }
159
160
161 private void updateLocalBundleMetadata( Artifact artifact, ObrUpdate update ) throws MojoExecutionException
162 {
163 if ( !supportedProjectTypes.contains( artifact.getType() ) )
164 {
165 return;
166 }
167 else if ( null == artifact.getFile() || artifact.getFile().isDirectory() )
168 {
169 getLog().error( "No artifact found, try \"mvn install bundle:install\"" );
170 return;
171 }
172
173 URI bundleJar = ObrUtils.getArtifactURI( localRepository, artifact );
174
175 URI sourceJar = null;
176 if ( null != m_sourceArtifact )
177 {
178 sourceJar = ObrUtils.getArtifactURI( localRepository, m_sourceArtifact );
179 }
180
181 URI docJar = null;
182 if ( null != m_docArtifact )
183 {
184 docJar = ObrUtils.getArtifactURI( localRepository, m_docArtifact );
185 }
186
187 update.updateRepository( bundleJar, sourceJar, docJar );
188 }
189 }