Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Maven 3.0 (drop Maven2 code) #122

Merged
merged 1 commit into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</parent>

<artifactId>exec-maven-plugin</artifactId>
<version>1.6.1-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>Exec Maven Plugin</name>
Expand Down Expand Up @@ -107,16 +107,6 @@
</scm>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-toolchain</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
Expand All @@ -127,11 +117,6 @@
<artifactId>maven-artifact</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact-manager</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
Expand Down Expand Up @@ -160,19 +145,31 @@
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-annotations</artifactId>
<version>1.6</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.3</version>
</dependency>

<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-artifact-transfer</artifactId>
<version>0.10.1</version>
</dependency>

<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>1.1</version>
<version>2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>${mavenVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -181,11 +178,16 @@
<version>1.21</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<mavenVersion>2.2.1</mavenVersion>
<mavenVersion>3.0</mavenVersion>
</properties>

<build>
Expand Down
35 changes: 16 additions & 19 deletions src/main/java/org/codehaus/mojo/exec/AbstractExecMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Resource;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
import org.codehaus.plexus.util.cli.CommandLineUtils;

/**
Expand All @@ -53,25 +52,19 @@ public abstract class AbstractExecMojo
*/
@Parameter( defaultValue = "${project}", readonly = true )
protected MavenProject project;

@Parameter( defaultValue = "${session}", readonly = true, required = true )
private MavenSession session;

@Component
private ArtifactResolver artifactResolver;

@Component
private ArtifactFactory artifactFactory;

@Component
private MavenProjectBuilder projectBuilder;

@Component
private ArtifactMetadataSource metadataSource;

@Parameter( readonly = true, required = true, defaultValue = "${localRepository}" )
private ArtifactRepository localRepository;

@Parameter( readonly = true, required = true, defaultValue = "${project.remoteArtifactRepositories}" )
private List<ArtifactRepository> remoteRepositories;
private ProjectBuilder projectBuilder;

@Parameter( defaultValue = "${plugin}", readonly = true ) // Maven 3 only
private PluginDescriptor plugin;

@Parameter( readonly = true, defaultValue = "${plugin.artifacts}" )
private List<Artifact> pluginDependencies;

Expand Down Expand Up @@ -152,7 +145,6 @@ public abstract class AbstractExecMojo
* @param artifacts the list where to collect the scope specific artifacts
* @param theClasspathFiles the list where to collect the scope specific output directories
*/
@SuppressWarnings( "unchecked" )
protected void collectProjectArtifactsAndClasspath( List<Artifact> artifacts, List<Path> theClasspathFiles )
{
if ( addResourcesToClasspath )
Expand Down Expand Up @@ -265,6 +257,11 @@ protected boolean isSkip()
return skip;
}

protected final MavenSession getSession()
{
return session;
}

/**
* Examine the plugin dependencies to find the executable artifact.
*
Expand Down
103 changes: 18 additions & 85 deletions src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,35 @@
package org.codehaus.mojo.exec;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.File;
import java.io.IOException;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.model.Dependency;
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.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.artifact.MavenMetadataSource;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;

/**
* Executes the supplied java class in the current VM with the enclosing project's dependencies as classpath.
Expand All @@ -66,31 +42,14 @@ public class ExecJavaMojo
extends AbstractExecMojo
{
@Component
private ArtifactResolver artifactResolver;

@Component
private ArtifactFactory artifactFactory;

@Component
private ArtifactMetadataSource metadataSource;
private DependencyResolver dependencyResolver;

/**
* @since 1.0
*/
@Parameter( readonly = true, required = true, defaultValue = "${localRepository}" )
private ArtifactRepository localRepository;

/**
* @since 1.1-beta-1
*/
@Parameter( readonly = true, required = true, defaultValue = "${project.remoteArtifactRepositories}" )
private List<ArtifactRepository> remoteRepositories;

/**
* @since 1.0
*/
@Component
private MavenProjectBuilder projectBuilder;
private ProjectBuilder projectBuilder;

/**
* @since 1.1-beta-1
Expand Down Expand Up @@ -645,8 +604,7 @@ private Set<Artifact> determineRelevantPluginDependencies()
{
getLog().debug( "Selected plugin Dependencies will be included." );
Artifact executableArtifact = this.findExecutableArtifact();
Artifact executablePomArtifact = this.getExecutablePomArtifact( executableArtifact );
relevantDependencies = this.resolveExecutableDependencies( executablePomArtifact );
relevantDependencies = this.resolveExecutableDependencies( executableArtifact );
}
}
else
Expand All @@ -657,19 +615,6 @@ private Set<Artifact> determineRelevantPluginDependencies()
return relevantDependencies;
}

/**
* Get the artifact which refers to the POM of the executable artifact.
*
* @param executableArtifact this artifact refers to the actual assembly.
* @return an artifact which refers to the POM of the executable artifact.
*/
private Artifact getExecutablePomArtifact( Artifact executableArtifact )
{
return this.artifactFactory.createBuildArtifact( executableArtifact.getGroupId(),
executableArtifact.getArtifactId(),
executableArtifact.getVersion(), "pom" );
}

/**
* Resolve the executable dependencies for the specified project
*
Expand All @@ -681,30 +626,18 @@ private Set<Artifact> resolveExecutableDependencies( Artifact executablePomArtif
throws MojoExecutionException
{

Set<Artifact> executableDependencies;
Set<Artifact> executableDependencies = new LinkedHashSet<>();
try
{
ProjectBuildingRequest buildingRequest = getSession().getProjectBuildingRequest();

MavenProject executableProject =
this.projectBuilder.buildFromRepository( executablePomArtifact, this.remoteRepositories,
this.localRepository );

// get all of the dependencies for the executable project
List<Dependency> dependencies = executableProject.getDependencies();

// make Artifacts of all the dependencies
Set<Artifact> dependencyArtifacts =
MavenMetadataSource.createArtifacts( this.artifactFactory, dependencies, null, null, null );

// not forgetting the Artifact of the project itself
dependencyArtifacts.add( executableProject.getArtifact() );

// resolve all dependencies transitively to obtain a comprehensive list of assemblies
ArtifactResolutionResult result =
artifactResolver.resolveTransitively( dependencyArtifacts, executablePomArtifact,
Collections.emptyMap(), this.localRepository,
this.remoteRepositories, metadataSource, null,
Collections.emptyList() );
executableDependencies = result.getArtifacts();
this.projectBuilder.build( executablePomArtifact, buildingRequest ).getProject();

for ( ArtifactResult artifactResult : dependencyResolver.resolveDependencies( buildingRequest, executableProject.getModel(), null ) )
{
executableDependencies.add( artifactResult.getArtifact() );
}
}
catch ( Exception ex )
{
Expand Down
Loading