Skip to content
Permalink
Browse files
(chore) cleanup from detatch -> detach (I'm bad at spelling)
  • Loading branch information
chtompki committed Jan 7, 2018
1 parent a47b6c0 commit 919f44a44780ea7f1e4062054f01e79fcb698130
Showing 7 changed files with 515 additions and 0 deletions.
@@ -0,0 +1,154 @@
/*
* 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.
*/
package org.apache.commons.release.plugin.mojos;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.release.plugin.SharedFunctions;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
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.artifact.Artifact;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
* The purpose of this maven mojo is to detach the artifacts generated by the maven-assembly-plugin,
* which for the Apache Commons Project do not get uploaded to Nexus, and putting those artifacts
* in the dev distribution location for apache projects.
*
* @author chtompki
* @since 1.0
*/
@Mojo( name = "detach-distributions", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
public class CommonsDistributionDetachmentMojo extends AbstractMojo {

/**
* A list of "artifact types" in the maven vernacular, to
* be detached from the deployment. For the time being we want
* all artifacts generated by the maven-assembly-plugin to be detatched
* from the deployment, namely *-src.zip, *-src.tar.gz, *-bin.zip,
* *-bin.tar.gz, and the corresponding .asc pgp signatures.
*/
private static final Set<String> ARTIFACT_TYPES_TO_DETATCH;
static {
Set<String> hashSet = new HashSet<>();
hashSet.add("zip");
hashSet.add("tar.gz");
hashSet.add("zip.asc");
hashSet.add("tar.gz.asc");
ARTIFACT_TYPES_TO_DETATCH = Collections.unmodifiableSet(hashSet);
}

/**
* This list is supposed to hold the maven references to the aformentioned artifacts so that we
* can upload them to svn after they've been detatched from the maven deployment.
*/
private List<Artifact> detatchedArtifacts = new ArrayList<>();

/**
* The maven project context injection so that we can get a hold of the variables at hand.
*/
@Parameter( defaultValue = "${project}", required = true )
private MavenProject project;

/**
*/
@Parameter( defaultValue = "${project.build.directory}/commons-release-plugin", alias = "outputDirectory" )
private File workingDirectory;

/**
*/
@Parameter(required = true)
private String distSvnStagingUrl;

public void execute() throws MojoExecutionException {
getLog().info("Detatching Assemblies");
for (Object attachedArtifact : project.getAttachedArtifacts()) {
if (ARTIFACT_TYPES_TO_DETATCH.contains(((Artifact) attachedArtifact).getType())) {
detatchedArtifacts.add((Artifact) attachedArtifact);
}
}
for(Artifact artifactToRemove : detatchedArtifacts) {
project.getAttachedArtifacts().remove(artifactToRemove);
}
if (!workingDirectory.exists()) {
SharedFunctions.initDirectory(getLog(), workingDirectory);
}
copyRemovedArtifactsToWorkingDirectory();
getLog().info("");
sha1AndMd5SignArtifacts();
}

private void copyRemovedArtifactsToWorkingDirectory() throws MojoExecutionException {
StringBuffer copiedArtifactAbsolutePath;
getLog().info("Copying detatched artifacts to working directory.");
for (Artifact artifact: detatchedArtifacts) {
File artifactFile = artifact.getFile();
copiedArtifactAbsolutePath = new StringBuffer(workingDirectory.getAbsolutePath());
copiedArtifactAbsolutePath.append("/");
copiedArtifactAbsolutePath.append(artifactFile.getName());
File copiedArtifact = new File(copiedArtifactAbsolutePath.toString());
getLog().info("Copying: " + artifactFile.getName());
SharedFunctions.copyFile(getLog(), artifactFile, copiedArtifact);
}
}

private void sha1AndMd5SignArtifacts() throws MojoExecutionException {
for (Artifact artifact : detatchedArtifacts) {
if (!artifact.getFile().getName().contains("asc")) {
try {
FileInputStream artifactFileInputStream = new FileInputStream(artifact.getFile());
String md5 = DigestUtils.md5Hex(artifactFileInputStream);
getLog().info(artifact.getFile().getName() + " md5: " + md5);
PrintWriter md5Writer = new PrintWriter(getMd5FilePath(workingDirectory, artifact.getFile()));
md5Writer.println(md5);
String sha1 = DigestUtils.sha1Hex(artifactFileInputStream);
getLog().info(artifact.getFile().getName() + " sha1: " + sha1);
PrintWriter sha1Writer = new PrintWriter(getSha1FilePath(workingDirectory, artifact.getFile()));
sha1Writer.println(sha1);
md5Writer.close();
sha1Writer.close();
} catch (IOException e) {
throw new MojoExecutionException("Could not sign file: " + artifact.getFile().getName(), e);
}
}
}
}

private String getMd5FilePath(File workingDirectory, File file) {
StringBuffer buffer = new StringBuffer(workingDirectory.getAbsolutePath());
buffer.append("/");
buffer.append(file.getName());
buffer.append(".md5");
return buffer.toString();
}

private String getSha1FilePath(File workingDirectory, File file) {
StringBuffer buffer = new StringBuffer(workingDirectory.getAbsolutePath());
buffer.append("/");
buffer.append(file.getName());
buffer.append(".sha1");
return buffer.toString();
}
}
@@ -0,0 +1,75 @@
/*
* 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.
*/
package org.apache.commons.release.plugin.mojos;

import org.apache.maven.plugin.testing.MojoRule;
import org.junit.Rule;
import org.junit.Test;

import java.io.File;

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

/**
* Unit tests for {@link CommonsDistributionDetachmentMojo}.
*
* @author chtompki
*/
public class CommonsDistributionDetachmentMojoTest {

@Rule
public MojoRule rule = new MojoRule() {
@Override
protected void before() throws Throwable {
}

@Override
protected void after() {
}
};

private CommonsDistributionDetachmentMojo mojo;

@Test
public void testSuccess() throws Exception {
File testPom = new File("src/test/resources/mojos/detach-distributions/detach-distributions.xml");
assertNotNull(testPom);
assertTrue(testPom.exists());
mojo = (CommonsDistributionDetachmentMojo) rule.lookupMojo("detach-distributions", testPom);
mojo.execute();
File detachedTarGz = new File("target/commons-release-plugin/mockAttachedTar.tar.gz");
File detachedTarGzAsc = new File("target/commons-release-plugin/mockAttachedTar.tar.gz.asc");
File detachedTarMd5 = new File("target/commons-release-plugin/mockAttachedTar.tar.gz.md5");
File detachedTarGzSha1 = new File("target/commons-release-plugin/mockAttachedTar.tar.gz.sha1");
File detachedZip = new File("target/commons-release-plugin/mockAttachedZip.zip");
File detachedZipAsc = new File("target/commons-release-plugin/mockAttachedZip.zip.asc");
File detachedZipMd5 = new File("target/commons-release-plugin/mockAttachedZip.zip.md5");
File detachedZipSha1 = new File("target/commons-release-plugin/mockAttachedZip.zip.sha1");
File notDetatchedMockAttachedFile = new File("target/commons-release-plugin/mockAttachedFile.html");
assertTrue(detachedTarGz.exists());
assertTrue(detachedTarGzAsc.exists());
assertTrue(detachedTarMd5.exists());
assertTrue(detachedTarGzSha1.exists());
assertTrue(detachedZip.exists());
assertTrue(detachedZipAsc.exists());
assertTrue(detachedZipMd5.exists());
assertTrue(detachedZipSha1.exists());
assertFalse(notDetatchedMockAttachedFile.exists());
}
}
@@ -0,0 +1,68 @@
/*
* 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.
*/
package org.apache.commons.release.plugin.mojos;

import org.apache.maven.plugin.testing.MojoRule;
import org.junit.Rule;
import org.junit.Test;

import java.io.File;

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertNotNull;

/**
* Unit tests for {@link CommonsDistributionStagingMojo}.
*
* @author chtompki
* @since 1.0.
*/
public class CommonsDistributionStagingMojoTest {

@Rule
public MojoRule rule = new MojoRule() {
@Override
protected void before() throws Throwable {
}

@Override
protected void after() {
}
};

private CommonsDistributionDetachmentMojo detatchmentMojo;

private CommonsDistributionStagingMojo mojoForTest;

@Test
public void testSuccess() throws Exception {
File testPom = new File("src/test/resources/mojos/stage-distributions/stage-distributions.xml");
assertNotNull(testPom);
assertTrue(testPom.exists());
File detachmentPom = new File("src/test/resources/mojos/detach-distributions/detach-distributions.xml");
assertNotNull(detachmentPom);
assertTrue(detachmentPom.exists());
mojoForTest = (CommonsDistributionStagingMojo) rule.lookupMojo("stage-distributions", testPom);
detatchmentMojo = (CommonsDistributionDetachmentMojo) rule.lookupMojo("detach-distributions", detachmentPom);
detatchmentMojo.execute();
File releaseNotesBasedir = new File("src/test/resources/mojos/stage-distributions/");
mojoForTest.setBasedir(releaseNotesBasedir);
mojoForTest.execute();
File targetScmDirectory = new File ("target/commons-release-plugin/scm");
assertTrue(targetScmDirectory.exists());
}
}
@@ -0,0 +1,92 @@
/*
* 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.
*/
package org.apache.commons.release.plugin.stubs;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.testing.stubs.ArtifactStub;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
* Stub for {@link MavenProject} for the {@link CommonsDistributionDetachmentMojoTest}.
*
* @author chtompki
*/
public class DistributionDetachmentProjectStub extends MavenProjectStub {

private List<Artifact> attachedArtifacts;

@Override
public List<Artifact> getAttachedArtifacts() {
attachedArtifacts = new ArrayList<>();
attachedArtifacts.add(
new DistributionDetatchmentArtifactStub(
new File("src/test/resources/mojos/detach-distributions/target/mockAttachedFile.html"),
"html"
)
);
attachedArtifacts.add(
new DistributionDetatchmentArtifactStub(
new File("src/test/resources/mojos/detach-distributions/target/mockAttachedTar.tar.gz"),
"tar.gz"
)
);
attachedArtifacts.add(
new DistributionDetatchmentArtifactStub(
new File("src/test/resources/mojos/detach-distributions/target/mockAttachedTar.tar.gz.asc"),
"tar.gz.asc"
)
);
attachedArtifacts.add(
new DistributionDetatchmentArtifactStub(
new File("src/test/resources/mojos/detach-distributions/target/mockAttachedZip.zip"),
"zip"
)
);
attachedArtifacts.add(
new DistributionDetatchmentArtifactStub(
new File("src/test/resources/mojos/detach-distributions/target/mockAttachedZip.zip.asc"),
"zip.asc"
)
);
return attachedArtifacts;
}

public class DistributionDetatchmentArtifactStub extends ArtifactStub {

private File artifact;

private String type;

public DistributionDetatchmentArtifactStub(File file, String type) {
this.artifact = file;
this.type = type;
}

public File getFile() {
return this.artifact;
}

public String getType() {
return this.type;
}
}
}

0 comments on commit 919f44a

Please sign in to comment.