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

[MINSTALL-175] Drop MAT #31

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 17 additions & 18 deletions pom.xml
Expand Up @@ -63,10 +63,10 @@
</distributionManagement>

<properties>
<mavenVersion>3.2.5</mavenVersion>
<slf4jVersion>1.7.32</slf4jVersion>
<aetherVersion>1.1.0</aetherVersion>
<javaVersion>7</javaVersion>
<mavenVersion>3.2.5</mavenVersion>
<aetherVersion>1.0.0.v20140518</aetherVersion> <!-- Maven bound -->

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is effectively a downgrade? From 1.1.0 to 1.0.0.v20140518?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because it aligns with the Aether version from Maven 3.2.5.

<slf4jVersion>1.7.5</slf4jVersion> <!-- Maven bound -->
<project.build.outputTimestamp>2020-04-07T21:04:00Z</project.build.outputTimestamp>
</properties>

Expand Down Expand Up @@ -97,9 +97,20 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-artifact-transfer</artifactId>
<version>0.13.1</version>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
<version>${aetherVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-util</artifactId>
<version>${aetherVersion}</version>
<scope>compile</scope> <!-- To work in Maven versions older than 3.9.0 -->
michael-o marked this conversation as resolved.
Show resolved Hide resolved
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>

<!-- dependencies to annotations -->
Expand Down Expand Up @@ -145,18 +156,6 @@
<version>${slf4jVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
<version>${aetherVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-util</artifactId>
<version>${aetherVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-impl</artifactId>
Expand Down
Expand Up @@ -21,14 +21,22 @@

import java.io.File;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
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.ProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifact;
import org.apache.maven.project.artifact.ProjectArtifactMetadata;
import org.apache.maven.shared.transfer.repository.RepositoryManager;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.installation.InstallRequest;
import org.eclipse.aether.installation.InstallationException;
import org.eclipse.aether.util.artifact.SubArtifact;

/**
* Common fields for installation mojos.
Expand All @@ -38,9 +46,8 @@
public abstract class AbstractInstallMojo
extends AbstractMojo
{

@Component
protected RepositoryManager repositoryManager;
protected RepositorySystem repositorySystem;

@Parameter( defaultValue = "${session}", required = true, readonly = true )
protected MavenSession session;
Expand All @@ -49,28 +56,98 @@ public abstract class AbstractInstallMojo
* Gets the path of the specified artifact within the local repository. Note that the returned path need not exist
* (yet).
*
* @param buildingRequest {@link ProjectBuildingRequest}.
* @param artifact The artifact whose local repo path should be determined, must not be <code>null</code>.
* @return The absolute path to the artifact when installed, never <code>null</code>.
*/
protected File getLocalRepoFile( ProjectBuildingRequest buildingRequest, Artifact artifact )
protected File getLocalRepoFile( Artifact artifact )
{
String path = repositoryManager.getPathForLocalArtifact( buildingRequest, artifact );
return new File( repositoryManager.getLocalRepositoryBasedir( buildingRequest ), path );
String path = session.getRepositorySession().getLocalRepositoryManager()
.getPathForLocalArtifact( RepositoryUtils.toArtifact( artifact ) );
return new File( session.getRepositorySession().getLocalRepository().getBasedir(), path );
}

/**
* Gets the path of the specified artifact metadata within the local repository. Note that the returned path need
* not exist (yet).
*
* @param buildingRequest {@link ProjectBuildingRequest}.
* @param metadata The artifact metadata whose local repo path should be determined, must not be <code>null</code>.
* @return The absolute path to the artifact metadata when installed, never <code>null</code>.
*/
protected File getLocalRepoFile( ProjectBuildingRequest buildingRequest, ProjectArtifactMetadata metadata )
protected File getLocalRepoFile( ProjectArtifactMetadata metadata )
{
String path = repositoryManager.getPathForLocalMetadata( buildingRequest, metadata );
return new File( repositoryManager.getLocalRepositoryBasedir( buildingRequest ), path );
DefaultArtifact pomArtifact = new DefaultArtifact(
metadata.getGroupId(),
metadata.getArtifactId(),
"",
"pom",
metadata.getBaseVersion() );

String path = session.getRepositorySession().getLocalRepositoryManager().getPathForLocalArtifact(
pomArtifact );
return new File( session.getRepositorySession().getLocalRepository().getBasedir(), path );
}

protected void installProject( MavenProject project )
throws MojoFailureException, MojoExecutionException
{
try
{
InstallRequest request = new InstallRequest();
Artifact artifact = project.getArtifact();
String packaging = project.getPackaging();
File pomFile = project.getFile();
boolean isPomArtifact = "pom".equals( packaging );

if ( pomFile != null )
{
request.addArtifact( RepositoryUtils.toArtifact( new ProjectArtifact( project ) ) );
}

if ( !isPomArtifact )
{
File file = artifact.getFile();

// Here, we have a temporary solution to MINSTALL-3 (isDirectory() is true if it went through compile

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks odd. We build against Maven 3.2.5 (see pom.xml) but this comment suggests a "proper solution" will be in place with Maven 2.1?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I lifted the relevant MAT code here, so this PR does not change "logic", it merely moves it into this plugin (that was before in MAT). So, just like in #15 "minimal change" is in place, that will subsequent PRs refine.

// but not package). We are designing in a proper solution for Maven 2.1
if ( file != null && file.isFile() )
{
org.eclipse.aether.artifact.Artifact mainArtifact = RepositoryUtils.toArtifact( artifact );
request.addArtifact( mainArtifact );

for ( Object metadata : artifact.getMetadataList() )
{
if ( metadata instanceof ProjectArtifactMetadata )
{
org.eclipse.aether.artifact.Artifact pomArtifact =
new SubArtifact( mainArtifact, "", "pom" );
pomArtifact = pomArtifact.setFile( ( (ProjectArtifactMetadata) metadata ).getFile() );
request.addArtifact( pomArtifact );
}
}
}
else if ( !project.getAttachedArtifacts().isEmpty() )
{
throw new MojoExecutionException( "The packaging plugin for this project did not assign "
+ "a main file to the project but it has attachments. Change packaging to 'pom'." );
}
else
{
throw new MojoExecutionException( "The packaging for this project did not assign "
+ "a file to the build artifact" );
}
}

for ( Artifact attached : project.getAttachedArtifacts() )
{
getLog().debug( "Attaching for install: " + attached.getId() );
request.addArtifact( RepositoryUtils.toArtifact( attached ) );
}

repositorySystem.install( session.getRepositorySession(), request );
}
catch ( InstallationException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
}
}
69 changes: 34 additions & 35 deletions src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java
Expand Up @@ -21,19 +21,20 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.building.ModelBuildingException;
Expand All @@ -53,14 +54,15 @@
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.project.artifact.ProjectArtifactMetadata;
import org.apache.maven.shared.transfer.project.install.ProjectInstaller;
import org.apache.maven.shared.transfer.project.install.ProjectInstallerRequest;
import org.apache.maven.shared.utils.Os;
import org.apache.maven.shared.utils.ReaderFactory;
import org.apache.maven.shared.utils.WriterFactory;
import org.apache.maven.shared.utils.io.IOUtil;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.codehaus.plexus.util.xml.XmlStreamWriter;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.DefaultRepositoryCache;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.LocalRepositoryManager;

/**
* Installs a file in the local repository.
Expand All @@ -71,6 +73,7 @@
public class InstallFileMojo
extends AbstractInstallMojo
{
private static final String LS = System.getProperty( "line.separator" );

/**
* GroupId of the artifact to be installed. Retrieved from POM file if one is specified or extracted from
Expand Down Expand Up @@ -170,36 +173,37 @@ public class InstallFileMojo
@Component
private ProjectBuilder projectBuilder;

/**
* Used to install the project created.
*/
@Component
private ProjectInstaller installer;

/**
* @see org.apache.maven.plugin.Mojo#execute()
*/
public void execute()
throws MojoExecutionException, MojoFailureException
{

if ( !file.exists() )
{
String message = "The specified file '" + file.getPath() + "' does not exist";
getLog().error( message );
throw new MojoFailureException( message );
}

ProjectBuildingRequest buildingRequest = session.getProjectBuildingRequest();

// ----------------------------------------------------------------------
// Override the default localRepository variable
// ----------------------------------------------------------------------
if ( localRepositoryPath != null )
{
buildingRequest = repositoryManager.setLocalRepositoryBasedir( buildingRequest, localRepositoryPath );

getLog().debug( "localRepoPath: " + repositoryManager.getLocalRepositoryBasedir( buildingRequest ) );
// "clone" repository session and replace localRepository
DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession(
session.getRepositorySession() );
// Clear cache, since we're using a new local repository
newSession.setCache( new DefaultRepositoryCache() );
// keep same repositoryType
String contentType = newSession.getLocalRepository().getContentType();
if ( "enhanced".equals( contentType ) )
{
contentType = "default";
}
LocalRepositoryManager localRepositoryManager = repositorySystem.newLocalRepositoryManager( newSession,
new LocalRepository( localRepositoryPath, contentType ) );
newSession.setLocalRepositoryManager( localRepositoryManager );
this.session = new MavenSession(
session.getContainer(), newSession, session.getRequest(), session.getResult() );
}

File temporaryPom = null;
Expand All @@ -225,10 +229,10 @@ public void execute()
project.getArtifact().setArtifactHandler( ah );
Artifact artifact = project.getArtifact();

if ( file.equals( getLocalRepoFile( buildingRequest, artifact ) ) )
if ( file.equals( getLocalRepoFile( artifact ) ) )
{
throw new MojoFailureException( "Cannot install artifact. "
+ "Artifact is already in the local repository.\n\nFile in question is: " + file + "\n" );
+ "Artifact is already in the local repository." + LS + LS + "File in question is: " + file + LS );
}

if ( classifier == null )
Expand Down Expand Up @@ -262,7 +266,7 @@ public void execute()
temporaryPom = generatePomFile();
ProjectArtifactMetadata pomMetadata = new ProjectArtifactMetadata( artifact, temporaryPom );
if ( Boolean.TRUE.equals( generatePom )
|| ( generatePom == null && !getLocalRepoFile( buildingRequest, pomMetadata ).exists() ) )
|| ( generatePom == null && !getLocalRepoFile( pomMetadata ).exists() ) )
{
getLog().debug( "Installing generated POM" );
if ( classifier == null )
Expand Down Expand Up @@ -293,12 +297,7 @@ else if ( generatePom == null )

try
{
// CHECKSTYLE_OFF: LineLength
ProjectInstallerRequest projectInstallerRequest =
new ProjectInstallerRequest().setProject( project );
// CHECKSTYLE_ON: LineLength

installer.install( buildingRequest, projectInstallerRequest );
installProject( project );
}
catch ( Exception e )
{
Expand Down Expand Up @@ -344,7 +343,7 @@ private MavenProject createMavenProject()
{
if ( e.getCause() instanceof ModelBuildingException )
{
throw new MojoExecutionException( "The artifact information is not valid:" + Os.LINE_SEP
throw new MojoExecutionException( "The artifact information is not valid:" + LS
+ e.getCause().getMessage() );
}
throw new MojoFailureException( "Unable to create the project.", e );
Expand Down Expand Up @@ -387,7 +386,7 @@ private File readingPomFromJarFile()
}
pomFile = File.createTempFile( base, ".pom" );

pomOutputStream = new FileOutputStream( pomFile );
pomOutputStream = Files.newOutputStream( pomFile.toPath() );

IOUtil.copy( pomInputStream, pomOutputStream );

Expand Down Expand Up @@ -448,7 +447,7 @@ private Model readModel( File pomFile )
Reader reader = null;
try
{
reader = ReaderFactory.newXmlReader( pomFile );
reader = new XmlStreamReader( pomFile );
final Model model = new MavenXpp3Reader().read( reader );
reader.close();
reader = null;
Expand Down Expand Up @@ -545,7 +544,7 @@ private File generatePomFile()
{
File pomFile = File.createTempFile( "mvninstall", ".pom" );

writer = WriterFactory.newXmlWriter( pomFile );
writer = new XmlStreamWriter( pomFile );
new MavenXpp3Writer().write( writer, model );
writer.close();
writer = null;
Expand Down