Skip to content

Commit

Permalink
Use the BuildPropertiesParser as plexus component only
Browse files Browse the repository at this point in the history
Currently we unnecessarily bind the build properties and interpolation
to the init of the Tycho model, this simply limits it usage and enforce
init of items that are probably never used.

This change the BuildProperties usage by a plexus component and remove
the helper methods from the ReactorPorject
  • Loading branch information
laeubi committed Dec 5, 2022
1 parent 7261ebf commit 190dd00
Show file tree
Hide file tree
Showing 22 changed files with 145 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*******************************************************************************/
package org.eclipse.tycho.p2.publisher;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
Expand All @@ -39,7 +38,6 @@
import org.eclipse.tycho.BuildPropertiesParser;
import org.eclipse.tycho.IArtifactFacade;
import org.eclipse.tycho.IDependencyMetadata.DependencyMetadataType;
import org.eclipse.tycho.Interpolator;
import org.eclipse.tycho.OptionalResolutionAction;
import org.eclipse.tycho.TargetEnvironment;
import org.eclipse.tycho.TychoConstants;
Expand All @@ -65,9 +63,8 @@ protected abstract List<IPublisherAction> getPublisherActions(IArtifactFacade ar

protected abstract List<IPublisherAdvice> getPublisherAdvice(IArtifactFacade artifact, PublisherOptions options);

protected ICapabilityAdvice getExtraEntriesAdvice(IArtifactFacade artifact, Interpolator interpolator) {
final IRequirement[] extraRequirements = extractExtraEntriesAsIURequirement(artifact.getLocation(),
interpolator);
protected ICapabilityAdvice getExtraEntriesAdvice(IArtifactFacade artifact, BuildProperties buildProps) {
final IRequirement[] extraRequirements = extractExtraEntriesAsIURequirement(buildProps);
return new ICapabilityAdvice() {
@Override
public boolean isApplicable(String configSpec, boolean includeDefault, String id, Version version) {
Expand All @@ -91,8 +88,8 @@ public IRequirement[] getMetaRequiredCapabilities(InstallableUnitDescription iu)
};
}

private IRequirement[] extractExtraEntriesAsIURequirement(File location, Interpolator interpolator) {
BuildProperties buildProps = getBuildPropertiesParser().parse(location, interpolator);
private IRequirement[] extractExtraEntriesAsIURequirement(BuildProperties buildProps) {

ArrayList<IRequirement> result = new ArrayList<>();
for (Entry<String, List<String>> entry : buildProps.getJarToExtraClasspathMap().entrySet()) {
createRequirementFromExtraClasspathProperty(result, entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.eclipse.tycho.BuildProperties;
import org.eclipse.tycho.BuildPropertiesParser;
import org.eclipse.tycho.IArtifactFacade;
import org.eclipse.tycho.Interpolator;
import org.eclipse.tycho.PackagingType;
import org.eclipse.tycho.p2.metadata.ReactorProjectFacade;

Expand Down Expand Up @@ -56,14 +55,17 @@ public FeatureRootAdvice(BuildProperties buildProperties, File baseDir, String a
*/
public static IFeatureRootAdvice createRootFileAdvice(IArtifactFacade featureArtifact,
BuildPropertiesParser buildPropertiesParser) {
Interpolator interpolator = featureArtifact instanceof ReactorProjectFacade reactorFacade
? reactorFacade.getReactorProject().getInterpolator()
: null;
File projectDir = getProjectBaseDir(featureArtifact);

if (projectDir != null) {
FeatureRootAdvice result = new FeatureRootAdvice(buildPropertiesParser.parse(projectDir, interpolator),
projectDir, featureArtifact.getArtifactId());

BuildProperties buildProperties;
if (featureArtifact instanceof ReactorProjectFacade reactorFacade) {
buildProperties = buildPropertiesParser.parse(reactorFacade.getReactorProject());
} else {
buildProperties = buildPropertiesParser.parse(projectDir, null);
}
FeatureRootAdvice result = new FeatureRootAdvice(buildProperties, projectDir,
featureArtifact.getArtifactId());
if (result.hasRootFiles()) {
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ public interface BuildPropertiesParser {

public static final String BUILD_PROPERTIES = "build.properties";

default BuildProperties parse(ReactorProject project) {
return parse(project.getBasedir(), project.getInterpolator());
}
BuildProperties parse(ReactorProject project);

/**
* Parse the file "build.properties" in baseDir. If the file does not exist or cannot be read,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package org.eclipse.tycho;

import java.io.File;
import java.util.Objects;
import java.util.Properties;
import java.util.function.Supplier;

Expand All @@ -23,8 +22,6 @@
*/
public interface ReactorProject extends IDependencyMetadata {

static final String CTX_INTERPOLATOR = "tycho.project.interpolator";
static final String CTX_BUILDPROPERTIESPARSER = "tycho.project.buildpropertiesparser";
static final String CTX_MERGED_PROPERTIES = "tycho.project.mergedProperties";

/**
Expand Down Expand Up @@ -78,25 +75,6 @@ default Properties getProperties() {
return (Properties) getContextValue(CTX_MERGED_PROPERTIES);
}

// misc
/**
*
* @return the Interpolator for this project that could be used to resolve maven variable
* references
*/
default Interpolator getInterpolator() {
return Objects.requireNonNull((Interpolator) getContextValue(CTX_INTERPOLATOR),
"No Interpolator found, has the TychoMavenLifecycleParticipant not run?");
}

default BuildProperties getBuildProperties() {
BuildPropertiesParser parser = Objects.requireNonNull(
(BuildPropertiesParser) getContextValue(CTX_BUILDPROPERTIESPARSER),
"No BuildPropertiesParser found, has the TychoMavenLifecycleParticipant not run?");
//we must always ask the parser here, it is expected that the parser caches the properties if the have not changed in the meanwhile
return parser.parse(this);
}

/**
* human-readable id used in error messages
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.eclipse.tycho.ArtifactKey;
import org.eclipse.tycho.ArtifactType;
import org.eclipse.tycho.BuildProperties;
import org.eclipse.tycho.BuildPropertiesParser;
import org.eclipse.tycho.DependencyArtifacts;
import org.eclipse.tycho.MavenArtifactRepositoryReference;
import org.eclipse.tycho.MavenDependencyDescriptor;
Expand All @@ -69,11 +70,12 @@ public final class MavenDependencyInjector {
* A project
* @param dependencies
* The p2-resolved dependencies of the project.
* @param buildPropertiesParser
*/
public static void injectMavenDependencies(MavenProject project, DependencyArtifacts dependencies,
DependencyArtifacts testDependencies, BundleReader bundleReader,
Function<ArtifactDescriptor, MavenDependencyDescriptor> descriptorMapping, Logger logger,
RepositorySystem repositorySystem, Settings settings) {
RepositorySystem repositorySystem, Settings settings, BuildPropertiesParser buildPropertiesParser) {
MavenDependencyInjector generator = new MavenDependencyInjector(project, bundleReader, descriptorMapping,
logger);
for (ArtifactDescriptor artifact : dependencies.getArtifacts()) {
Expand All @@ -85,7 +87,7 @@ public static void injectMavenDependencies(MavenProject project, DependencyArtif
.forEach(descriptor -> generator.addDependency(descriptor, Artifact.SCOPE_TEST));
}
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
BuildProperties buildProperties = reactorProject.getBuildProperties();
BuildProperties buildProperties = buildPropertiesParser.parse(reactorProject);
List<Dependency> extraJars = buildProperties.getJarsExtraClasspath().stream().map(extra -> {
if (TychoConstants.PLATFORM_URL_PATTERN.matcher(extra).matches()) {
//this should already be handled as an extra requirement!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
import org.eclipse.equinox.p2.metadata.IRequirement;
import org.eclipse.sisu.equinox.EquinoxServiceFactory;
import org.eclipse.tycho.BuildFailureException;
import org.eclipse.tycho.BuildPropertiesParser;
import org.eclipse.tycho.DependencyResolutionException;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.TychoConstants;
Expand Down Expand Up @@ -97,9 +96,6 @@ public class TychoMavenLifecycleParticipant extends AbstractMavenLifecyclePartic
@Requirement
private Logger log;

@Requirement
private BuildPropertiesParser buildPropertiesParser;

@Requirement
MavenProjectDependencyProcessor dependencyProcessor;

Expand Down Expand Up @@ -135,9 +131,6 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti

for (MavenProject project : projects) {
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
reactorProject.setContextValue(ReactorProject.CTX_INTERPOLATOR,
new TychoInterpolator(session, project));
reactorProject.setContextValue(ReactorProject.CTX_BUILDPROPERTIESPARSER, buildPropertiesParser);
resolver.setupProject(session, project, reactorProject);
}
if (TychoConstants.USE_OLD_RESOLVER) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,68 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.function.Supplier;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
import org.eclipse.tycho.BuildProperties;
import org.eclipse.tycho.BuildPropertiesParser;
import org.eclipse.tycho.Interpolator;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.core.maven.TychoInterpolator;
import org.eclipse.tycho.core.shared.BuildPropertiesImpl;

@Component(role = BuildPropertiesParser.class)
public class BuildPropertiesParserImpl implements BuildPropertiesParser, Disposable {

private final Map<String, BuildPropertiesImpl> cache = new HashMap<>();

public BuildPropertiesParserImpl() {
// empty to let plexus create new instances
}

/**
* Must only be used for tests!
*
* @param legacySupport
*/
protected BuildPropertiesParserImpl(LegacySupport legacySupport, Logger logger) {
}
@Requirement
LegacySupport legacySupport;

@Override
public BuildProperties parse(ReactorProject project) {
return parse(project.getBasedir(), project.getInterpolator());
}

public BuildProperties parse(File baseDir) {
return parse(baseDir, null);
return get(project.getBasedir(), () -> {
MavenProject mavenProject = project.adapt(MavenProject.class);
if (mavenProject != null) {
MavenSession session = legacySupport.getSession();
if (session != null) {
return new TychoInterpolator(session, mavenProject);
}
}
return null;
});
}

@Override
public BuildProperties parse(File baseDir, Interpolator interpolator) {
if (interpolator == null) {
return get(baseDir, () -> {
MavenSession session = legacySupport.getSession();
if (session != null) {
MavenProject currentProject = session.getCurrentProject();
if (currentProject != null) {
return new TychoInterpolator(session, currentProject);
}
}
return null;
});
}
return get(baseDir, () -> interpolator);
}

private synchronized BuildProperties get(File baseDir, Supplier<Interpolator> interpolatorSupplier) {
File propsFile = new File(baseDir, BUILD_PROPERTIES);
long lastModified = propsFile.lastModified();
String filePath = propsFile.getAbsolutePath();
BuildPropertiesImpl buildProperties = cache.get(filePath);
if (buildProperties == null || lastModified > buildProperties.getTimestamp()) {
Properties properties = readProperties(propsFile);
interpolate(properties, interpolator);
interpolate(properties, interpolatorSupplier.get());
buildProperties = new BuildPropertiesImpl(properties, lastModified);
cache.put(filePath, buildProperties);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.eclipse.osgi.util.ManifestElement;
import org.eclipse.tycho.ArtifactDescriptor;
import org.eclipse.tycho.ArtifactType;
import org.eclipse.tycho.BuildPropertiesParser;
import org.eclipse.tycho.DependencyArtifacts;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.TargetEnvironment;
Expand Down Expand Up @@ -92,6 +93,9 @@ public class EquinoxResolver {
@Requirement
private ToolchainManager toolchainManager;

@Requirement
private BuildPropertiesParser buildPropertiesParser;

public ModuleContainer newResolvedState(ReactorProject project, MavenSession mavenSession, ExecutionEnvironment ee,
DependencyArtifacts artifacts) throws BundleException {
Objects.requireNonNull(artifacts, "DependencyArtifacts can't be null!");
Expand Down Expand Up @@ -352,7 +356,8 @@ public ScheduledExecutorService getScheduledExecutor() {
} else {
ReactorProject mavenProject = artifact.getMavenProject();
if (mavenProject != null) {
Collection<String> additionalBundles = mavenProject.getBuildProperties().getAdditionalBundles();
Collection<String> additionalBundles = buildPropertiesParser.parse(mavenProject)
.getAdditionalBundles();
if (!additionalBundles.isEmpty()) {
List<String> reqb = new ArrayList<>();
String value = mf.getValue(Constants.REQUIRE_BUNDLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.eclipse.tycho.ArtifactDescriptor;
import org.eclipse.tycho.ArtifactKey;
import org.eclipse.tycho.ArtifactType;
import org.eclipse.tycho.BuildPropertiesParser;
import org.eclipse.tycho.DefaultArtifactKey;
import org.eclipse.tycho.DependencyArtifacts;
import org.eclipse.tycho.DependencyResolutionException;
Expand Down Expand Up @@ -132,6 +133,9 @@ public class OsgiBundleProject extends AbstractTychoProject implements BundlePro
@Requirement
private DeclarativeServiceConfigurationReader dsConfigReader;

@Requirement
private BuildPropertiesParser buildPropertiesParser;

@Override
public ArtifactDependencyWalker getDependencyWalker(ReactorProject project) {
final DependencyArtifacts artifacts = getDependencyArtifacts(project);
Expand Down Expand Up @@ -431,7 +435,7 @@ public EclipsePluginProjectImpl getEclipsePluginProject(ReactorProject otherProj
.getContextValue(TychoConstants.CTX_ECLIPSE_PLUGIN_PROJECT);
if (pdeProject == null) {
try {
pdeProject = new EclipsePluginProjectImpl(otherProject, otherProject.getBuildProperties(),
pdeProject = new EclipsePluginProjectImpl(otherProject, buildPropertiesParser.parse(otherProject),
classpathParser.parse(otherProject.getBasedir()));
if (otherProject instanceof DefaultReactorProject defaultReactorProject) {
populateProperties(defaultReactorProject.project.getProperties(), pdeProject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.equinox.p2.metadata.VersionRange;
import org.eclipse.equinox.p2.publisher.eclipse.BundlesAction;
import org.eclipse.tycho.BuildProperties;
import org.eclipse.tycho.BuildPropertiesParser;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.core.BundleProject;
import org.eclipse.tycho.core.TychoProject;
Expand All @@ -51,12 +52,15 @@ public class AdditionalBundleRequirementsInstallableUnitProvider implements Inst
@Requirement(role = TychoProject.class)
private Map<String, TychoProject> projectTypes;

@Requirement
private BuildPropertiesParser buildPropertiesParser;

@Override
public Collection<IInstallableUnit> getInstallableUnits(MavenProject project, MavenSession session)
throws CoreException {
if (projectTypes.get(project.getPackaging()) instanceof BundleProject) {
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
BuildProperties buildProperties = reactorProject.getBuildProperties();
BuildProperties buildProperties = buildPropertiesParser.parse(reactorProject);
List<IRequirement> additionalBundleRequirements = buildProperties.getAdditionalBundles().stream()
.map(bundleName -> MetadataFactory.createRequirement(BundlesAction.CAPABILITY_NS_OSGI_BUNDLE,
bundleName, VersionRange.emptyRange, null, true, true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.eclipse.tycho.ArtifactKey;
import org.eclipse.tycho.BuildFailureException;
import org.eclipse.tycho.BuildProperties;
import org.eclipse.tycho.BuildPropertiesParser;
import org.eclipse.tycho.DefaultArtifactKey;
import org.eclipse.tycho.DependencyArtifacts;
import org.eclipse.tycho.IDependencyMetadata;
Expand Down Expand Up @@ -126,6 +127,9 @@ public class P2DependencyResolver extends AbstractLogEnabled implements Dependen
@Requirement
private LocalRepositoryP2Indices p2index;

@Requirement
private BuildPropertiesParser buildPropertiesParser;

@Requirement
private PomUnits pomUnits;

Expand Down Expand Up @@ -338,7 +342,7 @@ private DependencyArtifacts doResolveDependencies(MavenSession session, MavenPro
}
}

BuildProperties buildProperties = DefaultReactorProject.adapt(project).getBuildProperties();
BuildProperties buildProperties = buildPropertiesParser.parse(DefaultReactorProject.adapt(project));
Collection<String> additionalBundles = buildProperties.getAdditionalBundles();
for (String additionalBundle : additionalBundles) {
resolver.addAdditionalBundleDependency(additionalBundle);
Expand Down Expand Up @@ -422,6 +426,6 @@ public void injectDependenciesIntoMavenModel(MavenProject project, AbstractTycho
DependencyArtifacts dependencyArtifacts, DependencyArtifacts testDependencyArtifacts, Logger logger) {
MavenDependencyInjector.injectMavenDependencies(project, dependencyArtifacts, testDependencyArtifacts,
bundleReader, resolverFactory::resolveDependencyDescriptor, logger, repositorySystem,
context.getSession().getSettings());
context.getSession().getSettings(), buildPropertiesParser);
}
}
Loading

0 comments on commit 190dd00

Please sign in to comment.