Skip to content

Commit

Permalink
Allow basedir in MavenExecutionContext and project-specific MavenImpl
Browse files Browse the repository at this point in the history
This allows to read and set project-specific properties that can be
passed to the Maven request and used for
further executions.

Covers parts of eclipse-m2e#666 and
progress towards eclipse-m2e#546
  • Loading branch information
mickaelistria committed May 16, 2022
1 parent c719290 commit 5d3e12f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 6 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.eclipse.m2e.tests.projects</groupId>
<artifactId>simplePomOK</artifactId>
<version>1</version>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.m2e.core.internal.MavenPluginActivator;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.MavenProjectInfo;
import org.eclipse.m2e.core.project.ProjectImportConfiguration;
import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;


Expand Down Expand Up @@ -78,5 +80,29 @@ public void testMNG6530() throws Exception {
} finally {
FileUtils.deleteDirectory(tempDirectory);
}
}

@Test
public void testMultiModuleProjectDirectoryChild() throws Exception {
IProject project = createExisting("simple", "resources/projects/dotMvn/", false);
waitForJobsToComplete(monitor);
IMavenProjectFacade facade = MavenPluginActivator.getDefault().getMavenProjectManagerImpl().create(project.getFile("child/pom.xml"),
true, monitor);
Assert.assertNotNull(facade);
File[] multiModuleDirectory = new File[] { null };
facade.getMaven().execute((context, monitor) -> multiModuleDirectory[0] = context.getExecutionRequest().getMultiModuleProjectDirectory(), null);
assertEquals(project.getLocation().toFile(), multiModuleDirectory[0]);
}

@Test
public void testMultiModuleProjectDirectorySimple() throws Exception {
IProject project = createExisting("hierarchyWithDotMVN", "resources/projects/simplePomOK", true);
waitForJobsToComplete(monitor);
IMavenProjectFacade facade = MavenPluginActivator.getDefault().getMavenProjectManagerImpl().create(project,
monitor);
Assert.assertNotNull(facade);
File[] multiModuleDirectory = new File[] { null };
facade.getMaven().execute((context, monitor) -> multiModuleDirectory[0] = context.getExecutionRequest().getMultiModuleProjectDirectory(), null);
assertEquals(project.getLocation().toFile(), multiModuleDirectory[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

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

import java.io.File;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
Expand Down Expand Up @@ -66,13 +67,17 @@ public class MavenExecutionContext implements IMavenExecutionContext {
// TODO maybe delegate to parent context
private Map<String, Object> context;

public MavenExecutionContext(MavenImpl maven) {
private final File basedir;

public MavenExecutionContext(MavenImpl maven, File basedir) {
this.maven = maven;
this.basedir = basedir == null ? null : (basedir.isDirectory() ? basedir : basedir.getParentFile());
}

MavenExecutionContext(MavenImpl maven, MavenExecutionRequest request) {
this.maven = maven;
this.request = request;
this.basedir = request.getBaseDirectory() != null ? new File(request.getBaseDirectory()) : null;
}

@Override
Expand All @@ -99,6 +104,7 @@ protected MavenExecutionRequest newExecutionRequest() throws CoreException {
if(request == null) {
request = maven.createExecutionRequest();
}
request.setMultiModuleProjectDirectory(MavenImpl.computeMultiModuleProjectDirectory(basedir));

return request;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@


@Component(service = {IMaven.class, IMavenConfigurationChangeListener.class})
public class MavenImpl implements IMaven, IMavenConfigurationChangeListener {
public class MavenImpl implements IMaven, IMavenConfigurationChangeListener, Cloneable {
private static final Logger log = LoggerFactory.getLogger(MavenImpl.class);

/**
Expand Down Expand Up @@ -204,6 +204,8 @@ public class MavenImpl implements IMaven, IMavenConfigurationChangeListener {
/** Last modified timestamp of cached user settings */
private long settingsTimestamp;

private File basedir;

MavenExecutionRequest createExecutionRequest() throws CoreException {
MavenExecutionRequest request = new DefaultMavenExecutionRequest();

Expand Down Expand Up @@ -242,6 +244,10 @@ MavenExecutionRequest createExecutionRequest() throws CoreException {
request.setGlobalChecksumPolicy(mavenConfiguration.getGlobalChecksumPolicy());
// the right way to disable snapshot update
// request.setUpdateSnapshots(false);
if(basedir != null) {
request.setBaseDirectory(basedir);
request.setMultiModuleProjectDirectory(computeMultiModuleProjectDirectory(basedir));
}
return request;
}

Expand Down Expand Up @@ -665,7 +671,7 @@ public Artifact resolve(String groupId, String artifactId, String version, Strin
private IMavenExecutionContext context() {
MavenExecutionContext context = MavenExecutionContext.getThreadContext();
if(context == null) {
context = new MavenExecutionContext(this);
context = createExecutionContext();
}
return context;
}
Expand Down Expand Up @@ -1250,7 +1256,7 @@ public void execute(MavenProject project, MojoExecution execution, IProgressMoni

@Override
public MavenExecutionContext createExecutionContext() {
return new MavenExecutionContext(this);
return new MavenExecutionContext(this, basedir);
}

@Override
Expand Down Expand Up @@ -1308,4 +1314,17 @@ public static File computeMultiModuleProjectDirectory(File file) {
return basedir;
}

public MavenImpl cloneForBasedir(File basedir) {
try {
MavenImpl res = (MavenImpl) clone();
res.basedir = basedir;
// TODO: more customization may be needed, more fields cleared, maybe in the end no
// need to start from a clone at all...
// TODO? What about getExecutionContext() that's tied to the thread and not to this?
return res;
} catch(CloneNotSupportedException ex) {
// unexpected, let's just crash
throw new RuntimeException(ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.m2e.core.embedder.ArtifactRef;
import org.eclipse.m2e.core.embedder.ArtifactRepositoryRef;
import org.eclipse.m2e.core.embedder.IMaven;
import org.eclipse.m2e.core.internal.embedder.MavenImpl;
import org.eclipse.m2e.core.lifecyclemapping.model.IPluginExecutionMetadata;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.MavenProjectUtils;
Expand Down Expand Up @@ -108,6 +109,8 @@ public class MavenProjectFacade implements IMavenProjectFacade, Serializable {

private transient Map<String, Object> sessionProperties;

private IMaven maven;

public MavenProjectFacade(ProjectRegistryManager manager, IFile pom, MavenProject mavenProject,
ResolverConfiguration resolverConfiguration) {
this.manager = manager;
Expand Down Expand Up @@ -532,7 +535,10 @@ public List<MojoExecution> getExecutionPlan(String lifecycle, IProgressMonitor m

@Override
public IMaven getMaven() {
// TODO: if project has Maven customization (such as a .mvn, MAVEN_OPTS...)
return MavenPlugin.getMaven();
if(maven == null) {
File basedir = pomFile != null ? (pomFile.isDirectory() ? pomFile : pomFile.getParentFile()) : null;
maven = ((MavenImpl) MavenPlugin.getMaven()).cloneForBasedir(basedir);
}
return maven;
}
}

0 comments on commit 5d3e12f

Please sign in to comment.