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

Fixes Issue #64 #70

Closed
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,23 @@

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.utils.ReaderFactory;
import org.apache.maven.shared.utils.WriterFactory;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
* Remove project's artifacts from local repository. Useful to keep only one copy of large local snapshot, for example:
Expand Down Expand Up @@ -89,6 +97,9 @@ public void execute()
if ( removeAll )
{
localArtifactDirectory = localArtifactDirectory.getParentFile();
} else {
File metadataFile = new File( localArtifactDirectory, "maven-metadata-local.xml" );
Copy link
Member

Choose a reason for hiding this comment

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

The "local" should be probably configurable ... but it can be done also in some future PR if someone would need it.
https://maven.apache.org/ref/3.2.5/maven-repository-metadata/index.html

modifyMetadataFile( this.project, metadataFile );
}

try
Expand All @@ -113,4 +124,23 @@ public void execute()
}
}
}

static void modifyMetadataFile( MavenProject mavenProject, File metadataFile ) throws MojoFailureException {
if ( metadataFile.exists() ) {
try {
Reader reader = ReaderFactory.newXmlReader( metadataFile );
Copy link
Member

Choose a reason for hiding this comment

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

Resource leak ... readers and writers should be closed: see https://programming.guide/java/try-with-resources.html for an example.

Copy link
Author

Choose a reason for hiding this comment

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

I could fix this, but there does not seem to be any activity for merging PRs here. I have already forgotten my personal usecase here.

Copy link

Choose a reason for hiding this comment

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

you could ping me, @olamy or @hboutemy.
Best approach is to post to the mailing list (google groups).

MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();
Metadata metadata = mappingReader.read( reader, true );
metadata.getVersioning().removeVersion(mavenProject.getArtifact().getVersion());
Writer writer = WriterFactory.newXmlWriter( metadataFile );
MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
mappingWriter.write( writer, metadata );
writer.flush();
} catch ( IOException e ) {
throw new MojoFailureException( "IOException thrown while trying to modify metadata file.", e );
} catch ( XmlPullParserException e ) {
throw new MojoFailureException( "XmlPullParserException thrown while trying to modify metadata file.", e );
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.codehaus.mojo.buildhelper;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.factory.DefaultArtifactFactory;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.junit.rules.TemporaryFolder;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class RemoveLocalArtifactMojoTest {

private static final String metadataContents = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<metadata>\n"
+ "<groupId>groupId</groupId>\n"
+ "<artifactId>artifactId</artifactId>\n"
+ "<versioning>\n"
+ "<release>3.0.1</release>\n"
+ "<versions>\n"
+ "<version>3.0.0</version>\n"
+ "<version>3.0.1</version>\n"
+ "</versions>\n"
+ "<lastUpdated>20180709210726</lastUpdated>\n"
+ "</versioning>\n"
+ "</metadata>";

@Test
public void testRemove300() throws MojoFailureException, IOException {

Path metadataFilePath = Files.createTempFile( "metadata", ".xml" );
File metadataFile = metadataFilePath.toFile();
try (PrintWriter out = new PrintWriter( metadataFile.getAbsolutePath() )) {
out.println( metadataContents );
}
MavenProject project = new MavenProject();
org.apache.maven.artifact.Artifact artifact = new DefaultArtifact( "groupId", "artifactId", "3.0.0", "compile",
"jar", "default", null );
project.setArtifact( artifact );

RemoveLocalArtifactMojo.modifyMetadataFile( project, metadataFile );
String newMetadata = readFile( metadataFile.toPath() );
Assert.assertEquals( artifact.getVersion(), "3.0.0" );
Assert.assertFalse( newMetadata.contains( "<version>3.0.0</version>" ) );
Assert.assertTrue( newMetadata.contains( "<version>3.0.1</version>" ) );
}

@Test
public void testRemove301() throws MojoFailureException, IOException {

Path metadataFilePath = Files.createTempFile( "metadata", ".xml" );
File metadataFile = metadataFilePath.toFile();
try (PrintWriter out = new PrintWriter( metadataFile.getAbsolutePath() )) {
out.println( metadataContents );
}
MavenProject project = new MavenProject();
org.apache.maven.artifact.Artifact artifact = new DefaultArtifact( "groupId", "artifactId", "3.0.1", "compile",
"jar", "default", null );
project.setArtifact( artifact );

RemoveLocalArtifactMojo.modifyMetadataFile( project, metadataFile );
String newMetadata = readFile( metadataFile.toPath() );
Assert.assertEquals( artifact.getVersion(), "3.0.1" );
Assert.assertTrue( newMetadata.contains( "<version>3.0.0</version>" ) );
Assert.assertFalse( newMetadata.contains( "<version>3.0.1</version>" ) );
}

private static String readFile(Path path) throws IOException {
byte[] encoded = Files.readAllBytes( path );
return new String( encoded, StandardCharsets.UTF_8 );
}
}