forked from eclipse-tycho/tycho
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MavenTargetDefinitionContent.java
278 lines (261 loc) · 15.6 KB
/
MavenTargetDefinitionContent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*******************************************************************************
* Copyright (c) 2020, 2021 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.p2.resolver;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.VersionedId;
import org.eclipse.equinox.p2.publisher.IPublisherInfo;
import org.eclipse.equinox.p2.publisher.PublisherInfo;
import org.eclipse.equinox.p2.publisher.eclipse.BundlesAction;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.tycho.core.resolver.shared.IncludeSourceMode;
import org.eclipse.tycho.core.shared.MavenDependenciesResolver;
import org.eclipse.tycho.core.shared.MavenLogger;
import org.eclipse.tycho.p2.metadata.IArtifactFacade;
import org.eclipse.tycho.p2.repository.GAV;
import org.eclipse.tycho.p2.target.TargetDefinitionContent;
import org.eclipse.tycho.p2.target.facade.TargetDefinition.BNDInstructions;
import org.eclipse.tycho.p2.target.facade.TargetDefinition.MavenDependency;
import org.eclipse.tycho.p2.target.facade.TargetDefinition.MavenGAVLocation;
import org.eclipse.tycho.p2.target.facade.TargetDefinition.MavenGAVLocation.MissingManifestStrategy;
import org.eclipse.tycho.p2.target.facade.TargetDefinitionResolutionException;
import org.eclipse.tycho.p2.target.repository.FileArtifactRepository;
import org.eclipse.tycho.p2.target.repository.SupplierMetadataRepository;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
@SuppressWarnings("restriction")
public class MavenTargetDefinitionContent implements TargetDefinitionContent {
public static final String ECLIPSE_SOURCE_BUNDLE_HEADER = "Eclipse-SourceBundle";
private final Map<IArtifactDescriptor, IInstallableUnit> repositoryContent = new HashMap<IArtifactDescriptor, IInstallableUnit>();
private SupplierMetadataRepository metadataRepository;
private FileArtifactRepository artifactRepository;
public MavenTargetDefinitionContent(MavenGAVLocation location, MavenDependenciesResolver mavenDependenciesResolver,
IncludeSourceMode sourceMode, IProvisioningAgent agent, MavenLogger logger) {
File repositoryRoot = mavenDependenciesResolver.getRepositoryRoot();
metadataRepository = new SupplierMetadataRepository(agent, () -> repositoryContent.values().iterator());
metadataRepository.setLocation(repositoryRoot.toURI());
metadataRepository.setName(repositoryRoot.getName());
artifactRepository = new FileArtifactRepository(agent, () -> repositoryContent.keySet().iterator());
artifactRepository.setName(repositoryRoot.getName());
artifactRepository.setLocation(repositoryRoot.toURI());
Collection<BNDInstructions> instructions = location.getInstructions();
if (mavenDependenciesResolver != null) {
logger.info("Resolving " + location + "...");
Map<String, Properties> instructionsMap = new HashMap<>();
for (BNDInstructions instruction : instructions) {
String reference = instruction.getReference();
Properties properties = instruction.getInstructions();
instructionsMap.put(reference, properties);
logger.info((reference.isEmpty() ? "default instructions" : reference) + " = " + properties);
}
for (MavenDependency mavenDependency : location.getRoots()) {
Collection<?> resolve = mavenDependenciesResolver.resolve(mavenDependency.getGroupId(),
mavenDependency.getArtifactId(), mavenDependency.getVersion(),
mavenDependency.getArtifactType(), mavenDependency.getClassifier(),
location.getIncludeDependencyScope());
Iterator<IArtifactFacade> resolvedArtifacts = resolve.stream().filter(IArtifactFacade.class::isInstance)
.map(IArtifactFacade.class::cast).iterator();
Properties defaultProperties = WrappedArtifact.createPropertiesForPrefix("wrapped");
while (resolvedArtifacts.hasNext()) {
IArtifactFacade mavenArtifact = resolvedArtifacts.next();
logger.debug("Resolved " + mavenArtifact + "...");
String symbolicName;
String bundleVersion;
try {
File bundleLocation = mavenArtifact.getLocation();
BundleDescription bundleDescription = BundlesAction.createBundleDescription(bundleLocation);
if (bundleDescription == null) {
throw new TargetDefinitionResolutionException("Artifact " + mavenArtifact + " of location "
+ location + " is not a valid jar file");
} else {
symbolicName = bundleDescription.getSymbolicName();
bundleVersion = bundleDescription.getVersion().toString();
IInstallableUnit unit;
if (symbolicName == null) {
if (location.getMissingManifestStrategy() == MissingManifestStrategy.IGNORE) {
logger.info("Ignoring " + asDebugString(mavenArtifact)
+ " as it is not a bundle and MissingManifestStrategy is set to ignore for this location");
continue;
}
if (location.getMissingManifestStrategy() == MissingManifestStrategy.ERROR) {
throw new TargetDefinitionResolutionException("Artifact "
+ asDebugString(mavenArtifact)
+ " is not a bundle and MissingManifestStrategy is set to error for this location");
}
File tempFile = File.createTempFile("tycho_wrapped_bundle", ".jar");
tempFile.deleteOnExit();
WrappedArtifact wrappedArtifact;
try {
Properties properties = instructionsMap.getOrDefault(getKey(mavenArtifact),
instructionsMap.getOrDefault("", defaultProperties));
wrappedArtifact = WrappedArtifact.createWrappedArtifact(mavenArtifact, properties,
tempFile);
} catch (Exception e) {
throw new TargetDefinitionResolutionException(
"Artifact " + asDebugString(mavenArtifact) + " could not be wrapped", e);
}
logger.info(asDebugString(mavenArtifact)
+ " is wrapped as a bundle with bundle symbolic name "
+ wrappedArtifact.getWrappedBsn());
logger.info(wrappedArtifact.getReferenceHint());
if (logger.isDebugEnabled()) {
logger.debug("The follwoing manifest was generated for this artifact:\r\n"
+ wrappedArtifact.getGeneratedManifest());
}
unit = publish(BundlesAction.createBundleDescription(tempFile), tempFile);
symbolicName = wrappedArtifact.getWrappedBsn();
bundleVersion = wrappedArtifact.getWrappedVersion();
} else {
unit = publish(bundleDescription, bundleLocation);
}
if (logger.isDebugEnabled()) {
logger.debug("MavenResolver: artifact " + asDebugString(mavenArtifact) + " at location "
+ bundleLocation + " resolves installable unit "
+ new VersionedId(unit.getId(), unit.getVersion()));
}
}
} catch (BundleException | IOException e) {
throw new TargetDefinitionResolutionException("Artifact " + asDebugString(mavenArtifact)
+ " of location " + location + " could not be read", e);
}
if (sourceMode == IncludeSourceMode.force
|| (sourceMode == IncludeSourceMode.honor && location.includeSource())) {
Collection<?> sourceArtifacts = mavenDependenciesResolver.resolve(mavenArtifact.getGroupId(),
mavenArtifact.getArtifactId(), mavenArtifact.getVersion(),
mavenArtifact.getPackagingType(), "sources", null);
Iterator<IArtifactFacade> sources = sourceArtifacts.stream()
.filter(IArtifactFacade.class::isInstance).map(IArtifactFacade.class::cast).iterator();
while (sources.hasNext()) {
IArtifactFacade sourceArtifact = sources.next();
File sourceFile = sourceArtifact.getLocation();
try {
Manifest manifest;
try (JarFile jar = new JarFile(sourceFile)) {
manifest = Objects.requireNonNullElseGet(jar.getManifest(), Manifest::new);
}
IInstallableUnit unit;
if (isValidSourceManifest(manifest)) {
unit = publish(BundlesAction.createBundleDescription(sourceFile), sourceFile);
} else {
unit = generateSourceBundle(symbolicName, bundleVersion, manifest, sourceFile);
}
if (unit != null && logger.isDebugEnabled()) {
logger.debug("MavenResolver: source-artifact " + asDebugString(sourceArtifact)
+ ":sources at location " + sourceFile + " resolves installable unit "
+ new VersionedId(unit.getId(), unit.getVersion()));
}
} catch (IOException | BundleException e) {
logger.warn("MavenResolver: source-artifact " + asDebugString(sourceArtifact)
+ ":sources at location " + sourceFile
+ " can't be converted to a source bundle: " + e);
continue;
}
}
}
}
}
}
}
private IInstallableUnit generateSourceBundle(String symbolicName, String bundleVersion, Manifest manifest,
File sourceFile) throws IOException, BundleException {
File tempFile = File.createTempFile("tycho_wrapped_source", ".jar");
tempFile.deleteOnExit();
Attributes attr = manifest.getMainAttributes();
if (attr.isEmpty()) {
attr.put(Name.MANIFEST_VERSION, "1.0");
}
attr.putValue(ECLIPSE_SOURCE_BUNDLE_HEADER, symbolicName + ";version=\"" + bundleVersion + "\";roots:=\".\"");
attr.putValue(Constants.BUNDLE_MANIFESTVERSION, "2");
attr.putValue(Constants.BUNDLE_NAME, "Source Bundle for " + symbolicName + ":" + bundleVersion);
attr.putValue(Constants.BUNDLE_SYMBOLICNAME, symbolicName + ".source");
attr.putValue(Constants.BUNDLE_VERSION, bundleVersion);
try (JarOutputStream stream = new JarOutputStream(new FileOutputStream(tempFile), manifest)) {
try (JarFile jar = new JarFile(sourceFile)) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = entries.nextElement();
if (jarEntry.getName().equals(JarFile.MANIFEST_NAME)) {
continue;
}
try (InputStream is = jar.getInputStream(jarEntry)) {
stream.putNextEntry(new ZipEntry(jarEntry.getName()));
is.transferTo(stream);
stream.closeEntry();
}
}
}
}
return publish(BundlesAction.createBundleDescription(tempFile), tempFile);
}
private IInstallableUnit publish(BundleDescription bundleDescription, File bundleLocation) {
IArtifactKey key = BundlesAction.createBundleArtifactKey(bundleDescription.getSymbolicName(),
bundleDescription.getVersion().toString());
PublisherInfo publisherInfo = new PublisherInfo();
publisherInfo.setArtifactOptions(IPublisherInfo.A_INDEX);
IInstallableUnit iu = BundlesAction.createBundleIU(bundleDescription, key, publisherInfo);
repositoryContent.put(FileArtifactRepository.forFile(bundleLocation, key), iu);
return iu;
}
private String asDebugString(IArtifactFacade mavenArtifact) {
return new GAV(mavenArtifact.getGroupId(), mavenArtifact.getArtifactId(), mavenArtifact.getVersion())
.toString();
}
@Override
public IArtifactRepository getArtifactRepository() {
return artifactRepository;
}
@Override
public IMetadataRepository getMetadataRepository() {
return metadataRepository;
}
private static String getKey(IArtifactFacade artifact) {
if (artifact == null) {
return "";
}
String key = artifact.getGroupId() + ":" + artifact.getArtifactId();
String classifier = artifact.getClassifier();
if (classifier != null) {
key += ":" + classifier;
}
key += ":" + artifact.getVersion();
return key;
}
private static boolean isValidSourceManifest(Manifest manifest) {
if (manifest != null) {
return manifest.getMainAttributes().getValue(ECLIPSE_SOURCE_BUNDLE_HEADER) != null;
}
return false;
}
}