Skip to content

Commit

Permalink
Minor clean-ups and simplifications in IMaven and MavenImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Oct 18, 2022
1 parent e7fd915 commit 0a23551
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ Artifact resolve(String groupId, String artifactId, String version, String type,
* Returns path of the specified artifact relative to repository baseDir. Can use used to access local repository
* files bypassing maven resolution logic.
*/
String getArtifactPath(ArtifactRepository repository, String groupId, String artifactId, String version,
String type, String classifier) throws CoreException;
String getArtifactPath(ArtifactRepository repository, String groupId, String artifactId, String version, String type,
String classifier) throws CoreException;

/**
* Returns true if the artifact does NOT exist in the local repository and known to be UNavailable from all specified
Expand All @@ -112,8 +112,7 @@ boolean isUnavailable(String groupId, String artifactId, String version, String
/**
* @since 1.10
*/
Map<File, MavenExecutionResult> readMavenProjects(Collection<File> pomFiles,
ProjectBuildingRequest configuration)
Map<File, MavenExecutionResult> readMavenProjects(Collection<File> pomFiles, ProjectBuildingRequest configuration)
throws CoreException;

/**
Expand Down Expand Up @@ -151,8 +150,8 @@ MojoExecution setupMojoExecution(MavenProject project, MojoExecution execution,
/**
* @since 1.4
*/
<T> T getMojoParameterValue(MavenProject project, MojoExecution mojoExecution, String parameter,
Class<T> asType, IProgressMonitor monitor) throws CoreException;
<T> T getMojoParameterValue(MavenProject project, MojoExecution mojoExecution, String parameter, Class<T> asType,
IProgressMonitor monitor) throws CoreException;

/**
* @since 1.4
Expand Down Expand Up @@ -223,8 +222,7 @@ <T> T getMojoParameterValue(MavenProject project, String parameter, Class<T> typ
* {@link #releaseMojo(Object, MojoExecution)}. This method is intended to allow introspection of mojo configuration
* parameters, use {@link #execute(MavenSession, MojoExecution, IProgressMonitor)} to execute mojo.
*/
<T> T getConfiguredMojo(MavenSession session, MojoExecution mojoExecution, Class<T> clazz)
throws CoreException;
<T> T getConfiguredMojo(MavenSession session, MojoExecution mojoExecution, Class<T> clazz) throws CoreException;

/**
* Releases resources used by Mojo acquired with {@link #getConfiguredMojo(MavenSession, MojoExecution, Class)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

import static org.eclipse.m2e.core.internal.M2EUtils.copyProperties;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -248,8 +248,7 @@ private MavenExecutionPlan calculateExecutionPlan(MavenSession session, List<Str
public MavenExecutionPlan calculateExecutionPlan(MavenProject project, List<String> goals, boolean setup,
IProgressMonitor monitor) throws CoreException {
return getExecutionContext().execute(project,
(context, pm) -> calculateExecutionPlan(context.getSession(), goals, setup),
monitor);
(context, pm) -> calculateExecutionPlan(context.getSession(), goals, setup), monitor);
}

private MojoExecution setupMojoExecution(MavenSession session, MavenProject project, MojoExecution execution)
Expand All @@ -273,8 +272,7 @@ private MojoExecution setupMojoExecution(MavenSession session, MavenProject proj
public MojoExecution setupMojoExecution(MavenProject project, MojoExecution execution, IProgressMonitor monitor)
throws CoreException {
return getExecutionContext().execute(project,
(context, pm) -> setupMojoExecution(context.getSession(), project, execution),
monitor);
(context, pm) -> setupMojoExecution(context.getSession(), project, execution), monitor);
}

@Override
Expand Down Expand Up @@ -548,9 +546,8 @@ private MavenProject resolveParentProject(RepositorySystemSession repositorySess
}

public MavenProject resolveParentProject(MavenProject child, IProgressMonitor monitor) throws CoreException {
return getExecutionContext().execute(child,
(context, pm) -> resolveParentProject(context.getRepositorySession(), child,
context.getExecutionRequest().getProjectBuildingRequest()), monitor);
return getExecutionContext().execute(child, (context, pm) -> resolveParentProject(context.getRepositorySession(),
child, context.getExecutionRequest().getProjectBuildingRequest()), monitor);
}

@Override
Expand Down Expand Up @@ -639,7 +636,7 @@ void setLastUpdated(ArtifactRepository localRepository, List<ArtifactRepository>

File lastUpdatedFile = getLastUpdatedFile(localRepository, artifact);
lastUpdatedFile.getParentFile().mkdirs();
try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(lastUpdatedFile))) {
try (OutputStream os = new FileOutputStream(lastUpdatedFile)) {
lastUpdated.store(os, null);
} catch(IOException ex) {
throw new CoreException(Status.error(Messages.MavenImpl_error_write_lastUpdated, ex));
Expand Down Expand Up @@ -703,7 +700,7 @@ private String getLastUpdatedKey(ArtifactRepository repository, Artifact artifac
private Properties loadLastUpdated(ArtifactRepository localRepository, Artifact artifact) throws CoreException {
Properties lastUpdated = new Properties();
File lastUpdatedFile = getLastUpdatedFile(localRepository, artifact);
try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(lastUpdatedFile))) {
try (InputStream is = new FileInputStream(lastUpdatedFile)) {
lastUpdated.load(is);
} catch(FileNotFoundException ex) {
// that's okay
Expand All @@ -714,20 +711,9 @@ private Properties loadLastUpdated(ArtifactRepository localRepository, Artifact
}

private File getLastUpdatedFile(ArtifactRepository localRepository, Artifact artifact) {
return new File(localRepository.getBasedir(), basePathOf(artifact) + "/m2e-lastUpdated.properties");
}

private static final char PATH_SEPARATOR = '/';

private static final char GROUP_SEPARATOR = '.';

private String basePathOf(Artifact artifact) {
return formatAsDirectory(artifact.getGroupId()) + PATH_SEPARATOR + artifact.getArtifactId() + PATH_SEPARATOR
+ artifact.getBaseVersion() + PATH_SEPARATOR;
}

private String formatAsDirectory(String directory) {
return directory.replace(GROUP_SEPARATOR, PATH_SEPARATOR);
return Path.of(localRepository.getBasedir(), //
artifact.getGroupId().replace('.', '/'), artifact.getArtifactId(), artifact.getBaseVersion(),
"m2e-lastUpdated.properties").toFile();
}

private <T> T getMojoParameterValue(MavenSession session, MojoExecution mojoExecution, String parameter,
Expand Down Expand Up @@ -1050,7 +1036,6 @@ public <C> Collection<C> lookupCollection(Class<C> type) throws CoreException {
}
}


@Override
public ClassLoader getProjectRealm(MavenProject project) {
Objects.requireNonNull(project);
Expand Down Expand Up @@ -1085,8 +1070,7 @@ public void interpolateModel(MavenProject project, Model model) throws CoreExcep
* @since 1.4
*/
public static <V> V execute(IMaven maven, boolean offline, boolean forceDependencyUpdate, ICallable<V> callable,
IProgressMonitor monitor)
throws CoreException {
IProgressMonitor monitor) throws CoreException {
IMavenExecutionContext context = maven.createExecutionContext();
context.getExecutionRequest().setOffline(offline);
context.getExecutionRequest().setUpdateSnapshots(forceDependencyUpdate);
Expand Down

0 comments on commit 0a23551

Please sign in to comment.