Skip to content

Commit

Permalink
Add option to include all configured sources in ApiFileGenerationMojo
Browse files Browse the repository at this point in the history
Currently APIFileGenerator scans all folders from the root, but one can
configured folders that are relative to the project, this is then not
discovered.

This adds a new option <addSourceFolders> that when used automatically
adds all configured source folders as extraSourceLocations.
  • Loading branch information
laeubi committed Mar 9, 2024
1 parent 1f15d60 commit 517ea22
Showing 1 changed file with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
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;
Expand All @@ -27,7 +30,9 @@
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;

/**
Expand Down Expand Up @@ -84,9 +89,20 @@ public class ApiFileGenerationMojo extends AbstractMojo {
@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);
Expand All @@ -110,13 +126,32 @@ public void execute() throws MojoExecutionException, MojoFailureException {
generator.encoding = encoding;
generator.debug = debug;
generator.manifests = join(extraManifests);
generator.sourceLocations = join(extraSourceLocations);
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 String join(List<File> list) {
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));
}
Expand Down

0 comments on commit 517ea22

Please sign in to comment.