Skip to content

Commit

Permalink
Cleanup AbstractUpdateMojo
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Aug 15, 2024
1 parent 39a21e8 commit e6b7f55
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,47 @@
package org.eclipse.tycho.versionbump;

import java.io.File;
import java.util.Collections;

import org.apache.maven.execution.MavenSession;
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.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.tycho.TargetEnvironment;
import org.eclipse.tycho.core.resolver.P2Resolver;
import org.eclipse.tycho.core.resolver.P2ResolverFactory;

public abstract class AbstractUpdateMojo extends AbstractMojo {

@Component
private Logger logger;

@Parameter(defaultValue = "JavaSE-17")
private String executionEnvironment;

@Parameter(property = "project")
private MavenProject project;

@Component
private P2ResolverFactory factory;
private MavenSession mavenSession;

MavenSession getMavenSession() {
return mavenSession;
}

public void execute() throws MojoExecutionException, MojoFailureException {
try {
doUpdate();
} catch (Exception e) {
if (e instanceof MojoFailureException mfe) {
throw mfe;
}
if (e instanceof MojoExecutionException mee) {
throw mee;
File file = getFileToBeUpdated();
if (file != null) {
try {
doUpdate(file);
} catch (Exception e) {
if (e instanceof MojoFailureException mfe) {
throw mfe;
}
if (e instanceof MojoExecutionException mee) {
throw mee;
}
throw new MojoExecutionException("Could not update " + getFileToBeUpdated(), e);
}
throw new MojoExecutionException("Could not update " + getFileToBeUpdated(), e);
}
}

protected abstract File getFileToBeUpdated() throws MojoExecutionException, MojoFailureException;

protected abstract void doUpdate() throws Exception;

P2Resolver createResolver() {
return factory.createResolver(Collections.singletonList(TargetEnvironment.getRunningEnvironment()));

}

String getExecutionEnvironment() {
return executionEnvironment;
}
protected abstract void doUpdate(File file) throws Exception;

MavenProject getProject() {
return project;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.tycho.ArtifactType;
import org.eclipse.tycho.IllegalArtifactReferenceException;
import org.eclipse.tycho.MavenRepositoryLocation;
import org.eclipse.tycho.TargetEnvironment;
import org.eclipse.tycho.core.ee.shared.ExecutionEnvironmentConfigurationStub;
import org.eclipse.tycho.core.resolver.P2ResolutionResult;
import org.eclipse.tycho.core.resolver.P2Resolver;
import org.eclipse.tycho.core.resolver.P2ResolverFactory;
import org.eclipse.tycho.model.PluginRef;
import org.eclipse.tycho.model.ProductConfiguration;
import org.eclipse.tycho.p2.target.facade.TargetPlatformConfigurationStub;
Expand All @@ -45,8 +49,23 @@ public class UpdateProductMojo extends AbstractUpdateMojo {
@Parameter(defaultValue = "${project.artifactId}.product")
private File productFile;

@Parameter(defaultValue = "JavaSE-17")
private String executionEnvironment;

@Component
private P2ResolverFactory factory;

String getExecutionEnvironment() {
return executionEnvironment;
}

P2Resolver createResolver() {
return factory.createResolver(Collections.singletonList(TargetEnvironment.getRunningEnvironment()));

}

@Override
protected void doUpdate() throws IOException, URISyntaxException {
protected void doUpdate(File file) throws IOException, URISyntaxException {

TargetPlatformConfigurationStub resolutionContext = new TargetPlatformConfigurationStub();
for (ArtifactRepository repository : getProject().getRemoteArtifactRepositories()) {
Expand All @@ -56,7 +75,7 @@ protected void doUpdate() throws IOException, URISyntaxException {
}
}

ProductConfiguration product = ProductConfiguration.read(productFile);
ProductConfiguration product = ProductConfiguration.read(file);

P2Resolver resolver = createResolver();
for (PluginRef plugin : product.getPlugins()) {
Expand All @@ -83,7 +102,7 @@ protected void doUpdate() throws IOException, URISyntaxException {
}
}

ProductConfiguration.write(product, productFile);
ProductConfiguration.write(product, file);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,15 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URISyntaxException;
import java.util.List;

import javax.inject.Inject;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.VersionRangeResolutionException;
import org.eclipse.aether.version.InvalidVersionSpecificationException;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.tycho.targetplatform.TargetPlatformArtifactResolver;
import org.eclipse.tycho.targetplatform.TargetResolveException;

Expand Down Expand Up @@ -80,20 +71,14 @@ public class UpdateTargetMojo extends AbstractUpdateMojo {
@Parameter(property = "discovery")
private String updateSiteDiscovery;

@Component
private MavenSession mavenSession;

@Inject
private MavenLocationUpdater mavenLocationUpdater;

@Inject
private InstallableUnitLocationUpdater installableUnitLocationUpdater;

@Override
protected void doUpdate() throws IOException, URISyntaxException, ParserConfigurationException,
TargetResolveException, MojoFailureException, VersionRangeResolutionException, ArtifactResolutionException,
InvalidVersionSpecificationException, ProvisionException {
File file = getFileToBeUpdated();
protected void doUpdate(File file) throws Exception {
getLog().info("Update target file " + file);
//we use the descent xml parser here because we need to retain the formating of the original file
XMLParser parser = new XMLParser();
Expand Down Expand Up @@ -164,10 +149,6 @@ boolean isUpdateMajorVersion() {
return updateMajorVersion;
}

MavenSession getMavenSession() {
return mavenSession;
}

String getUpdateSiteDiscovery() {
return updateSiteDiscovery;
}
Expand Down

0 comments on commit e6b7f55

Please sign in to comment.