-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathApiFileGenerationMojo.java
More file actions
161 lines (139 loc) · 4.98 KB
/
Copy pathApiFileGenerationMojo.java
File metadata and controls
161 lines (139 loc) · 4.98 KB
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
/*******************************************************************************
* Copyright (c) 2022 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.apitools;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import javax.inject.Inject;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.eclipse.pde.api.tools.internal.APIFileGenerator;
import org.eclipse.tycho.BuildPropertiesParser;
import org.eclipse.tycho.core.TychoProjectManager;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.model.project.EclipseProject;
/**
* performs generation of PDE-API Tools description
*/
@Mojo(name = "generate", defaultPhase = LifecyclePhase.PROCESS_CLASSES, threadSafe = true)
public class ApiFileGenerationMojo extends AbstractMojo {
@Inject
protected MavenProject project;
@Parameter(defaultValue = "${project.build.directory}")
protected File targetFolder;
@Parameter(defaultValue = "${project.build.outputDirectory}")
protected File binaryLocations;
@Parameter(defaultValue = "${project.basedir}")
protected File projectLocation;
@Parameter(defaultValue = "${project.artifactId}_${project.version}")
protected String projectName;
/**
* @Since 3.1.0
*/
@Parameter(defaultValue = "false")
private boolean allowNonApiProject;
/**
* @Since 3.1.0
*/
@Parameter
protected String encoding;
/**
* @Since 3.1.0
*/
@Parameter
protected boolean debug;
/**
* @Since 3.1.0
*/
@Parameter
protected List<File> extraManifests = List.of();
/**
* @Since 3.1.0
*/
@Parameter
protected List<File> extraSourceLocations = List.of();
@Parameter(defaultValue = "false", property = "tycho.apitools.generate.skip")
private boolean skip;
/**
* If set to
* <code>true</true> all configured source folders in <code>build.properties</code>
* will be added as {@link #extraSourceLocations}
*/
@Parameter(defaultValue = "false")
private boolean addSourceFolders;
@Component
private TychoProjectManager projectManager;
@Component
private BuildPropertiesParser buildPropertiesParser;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Optional<EclipseProject> eclipseProject = projectManager.getEclipseProject(project);
if (eclipseProject.isEmpty()
|| !eclipseProject.get().hasNature("org.eclipse.pde.api.tools.apiAnalysisNature")) {
return;
}
if (new File(project.getBasedir(), JarFile.MANIFEST_NAME).isFile()
|| extraManifests.stream().anyMatch(File::isFile)) {
synchronized (ApiFileGenerationMojo.class) {
// TODO check if the generator is thread safe, then we can remove this!
if (!binaryLocations.exists()) {
binaryLocations.mkdirs();
}
APIFileGenerator generator = new APIFileGenerator();
generator.projectName = projectName;
generator.projectLocation = projectLocation.getAbsolutePath();
generator.binaryLocations = binaryLocations.getAbsolutePath();
generator.targetFolder = targetFolder.getAbsolutePath();
generator.allowNonApiProject = allowNonApiProject;
generator.encoding = encoding;
generator.debug = debug;
generator.manifests = join(extraManifests);
if (addSourceFolders) {
List<File> list = new ArrayList<>(extraSourceLocations);
for (Map.Entry<String, List<String>> entry : buildPropertiesParser
.parse(DefaultReactorProject.adapt(project)).getJarToSourceFolderMap().entrySet()) {
for (String sourceFolder : entry.getValue()) {
list.add(canonicalFile(new File(project.getBasedir(), sourceFolder)));
}
}
generator.sourceLocations = join(list);
} else {
generator.sourceLocations = join(extraSourceLocations);
}
generator.generateAPIFile();
}
}
}
private static File canonicalFile(File file) {
try {
return file.getCanonicalFile();
} catch (IOException e) {
}
return file;
}
private static String join(List<File> list) {
return list.isEmpty() ? null // join the elements so that the APIFileGenerator splits it correspondingly
: list.stream().map(File::toString).collect(Collectors.joining(File.pathSeparator));
}
}