Skip to content

Commit

Permalink
update configuration settings; change exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chonton committed Jun 14, 2019
1 parent 41a3eb4 commit 23d8cdc
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 112 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ if the just built artifact is already in the local repository;
and [remote](https://chonton.github.io/exists-maven-plugin/0.0.7/remote-mojo.html) checks
if the just built artifact is already in the remote repository.

| Parameter | Default | Description |
|-----------|---------|-------------|
|project |${project.groupId}:${project.artifactId}:${project.version}| The project within the repository to query|
|artifact |${project.artifactId}-${project.version}.{packaging}|The artifact within the project to query|
|property |maven.deploy.skip|The property to receive the result of the query|
|userProperty|false|If the property should be set as a user property, to be available in child projects|
|useChecksum|${createChecksum}|Use checksum to compare artifacts (Checksums only available when install plugin is so configured.)|
|skipIfSnapshot|true|If checksums are not used, skip the query if the project ends with -SNAPSHOT|
|repository |${project.distributionManagement.repository.url}| For remote goal, the repository to query for artifacts|
|snapshotRepository |${project.distributionManagement.snapshotRepository.url}| For remote goal, the repository to query for snapshot artifacts|
|serverId|${project.distributionManagement.repository.id}|For remote goal, the server ID to use for authentication and proxy settings|
|snapshotServerId|${project.distributionManagement.snapshotRepository.id}|For remote goal, the server ID to use for snapshot authentication and proxy settings|
|failIfExists|${failIfExists}|Fail the build if the artifact already exists|
|failIfNotExists|${failIfNotExists}|Fail the build if the artifact does not exist|
|failIfNotMatches|${failIfNotMatches}|Fail the build if the artifact exists and useChecksum is set and checksums do not match|
|skip|${exists.skip}|Skip executing the plugin|
| Parameter | Property | Default | Description |
|-----------|---------|---------|-------------|
|project |exists.project|${project.groupId}:${project.artifactId}:${project.version}| The project within the repository to query|
|artifact |exists.artifact|${project.artifactId}-${project.version}.{packaging}|The artifact within the project to query|
|property |exists.property|${maven.deploy.skip} / ${maven.install.skip}|The property to receive the result of the query|
|userProperty|exists.userProperty|false|If the property should be set as a user property, to be available in child projects|
|useChecksum|exists.useChecksum|true|Use checksum to compare artifacts|
|skipIfSnapshot|exists.skipIfSnapshot|true|If checksums are not used, skip the query if the project ends with -SNAPSHOT|
|repository |exists.repository|${project.distributionManagement.repository.url}| For remote goal, the repository to query for artifacts|
|snapshotRepository|exists.snapshotRepository|${project.distributionManagement.snapshotRepository.url}| For remote goal, the repository to query for snapshot artifacts|
|serverId|exists.serverId|${project.distributionManagement.repository.id}|For remote goal, the server ID to use for authentication and proxy settings|
|snapshotServerId|exists.snapshotServerId|${project.distributionManagement.snapshotRepository.id}|For remote goal, the server ID to use for snapshot authentication and proxy settings|
|failIfExists|exists.failIfExists|${failIfExists}|Fail the build if the artifact already exists|
|failIfNotExists|exists.failIfNotExists|${failIfNotExists}|Fail the build if the artifact does not exist|
|failIfNotMatch|exists.failIfNotMatch|${failIfNotMatches}|Fail the build if the artifact exists and useChecksum is set and checksums do not match|
|skip|exists.skip|false|Skip executing the plugin|

Typical use:

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<version>3.0.0-M1</version>
<executions>
<!-- install in earlier than usual phase -->
<execution>
Expand Down
2 changes: 0 additions & 2 deletions src/it/deploy/verify.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ import java.nio.file.Files;
BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8);

findLine(reader, "\\[INFO\\] --- exists-maven-plugin:[0-9\\.]*:remote \\(before-installation\\) @ exists-deploy-it ---");
findLine(reader, "\\[INFO\\] setting maven.deploy.skip=false");
findLine(reader, "\\[INFO\\] --- exists-maven-plugin:[0-9\\.]*:remote \\(before-installation-with-auth\\) @ exists-deploy-it ---");
findLine(reader, "\\[INFO\\] setting maven.deploy.skip=false");
findLine(reader, "\\[INFO\\] --- exists-maven-plugin:[0-9\\.]*:remote \\(after-installation\\) @ exists-deploy-it ---");
findLine(reader, "\\[INFO\\] setting maven.deploy.skip=true");
findLine(reader, "\\[INFO\\] --- exists-maven-plugin:[0-9\\.]*:remote \\(after-installation-with-auth\\) @ exists-deploy-it ---");
Expand Down
1 change: 0 additions & 1 deletion src/it/fail/verify.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ import java.nio.file.Files;
BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8);

findLine(reader, "\\[INFO\\] --- exists-maven-plugin:[0-9\\.]*:local \\(before-installation\\) @ exists-fail-it ---");
findLine(reader, "\\[INFO\\] setting maven.install.skip=false");
findLine(reader, "\\[INFO\\] --- exists-maven-plugin:[0-9\\.]*:local \\(after-installation\\) @ exists-fail-it ---");
findLine(reader, "\\[ERROR\\] Failed to execute goal org.honton.chas:exists-maven-plugin:[0-9\\.]*:local \\(after-installation\\) on project exists-fail-it: Artifact already exists in repository: org.honton.chas:exists-fail-it:[0-9\\.]*/exists-fail-it-[0-9\\.]*.jar -> \\[Help 1\\]");
1 change: 0 additions & 1 deletion src/it/install/verify.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import java.nio.file.Files;
BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8);

findLine(reader, "\\[INFO\\] --- exists-maven-plugin:[0-9\\.]*:local \\(before-installation\\) @ exists-install-it ---");
findLine(reader, "\\[INFO\\] setting maven.install.skip=false");
findLine(reader, "\\[INFO\\] --- exists-maven-plugin:[0-9\\.]*:local \\(after-installation\\) @ exists-install-it ---");
findLine(reader, "\\[INFO\\] setting maven.install.skip=true");

Expand Down
11 changes: 3 additions & 8 deletions src/it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@

<properties>

<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!-- don't deploy -->
<maven.deploy.skip>true</maven.deploy.skip>
<!-- don't install -->
<maven.install.skip>true</maven.install.skip>
<maven.site.deploy.skip>true</maven.site.deploy.skip>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<mockserver.version>3.10.4</mockserver.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -60,7 +55,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<version>3.0.0-M1</version>
</plugin>

<plugin>
Expand Down
78 changes: 37 additions & 41 deletions src/main/java/org/honton/chas/exists/AbstractExistsMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,35 @@
*/
public abstract class AbstractExistsMojo extends AbstractMojo {

@Parameter(defaultValue = "${project}", readonly = true)
@Parameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject mavenProject;

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

/**
* The project Group:Artifact:Version to compare. Defaults to the current project's GAV.
*/
@Parameter(defaultValue = "${project.groupId}:${project.artifactId}:${project.version}")
@Parameter(property = "exists.project", defaultValue = "${project.groupId}:${project.artifactId}:${project.version}")
private String project;

/**
* The artifact of the project to compare. Defaults to the project's principal artifact.
*/
@Parameter(defaultValue = "${project.build.finalName}.${project.packaging}")
@Parameter(property = "exists.artifact", defaultValue = "${project.build.finalName}.${project.packaging}")
private String artifact;

/**
* Set whether checksum is used to compare artifacts. The default is the <em>install</em>
* plugin's configuration to create checksums (property <em>createChecksum</em>).
*/
@Parameter(defaultValue = "${createChecksum}")
@Parameter(property = "exists.useChecksum", defaultValue = "true")
private boolean useChecksum;

/**
* If checksums are not used, should this plugin skip checking SNAPSHOT versions?
*/
@Parameter(defaultValue = "true")
@Parameter(property = "exists.skipIfSnapshot", defaultValue = "true")
private boolean skipIfSnapshot;

/**
Expand All @@ -53,50 +56,46 @@ public abstract class AbstractExistsMojo extends AbstractMojo {
*
* @since 0.0.3
*/
@Parameter(defaultValue = "false")
@Parameter(property = "exists.userProperty", defaultValue = "false")
private boolean userProperty;

/**
* Fail the build if the artifact already exists in the repository.
*
* @since 0.0.3
*/
@Parameter(defaultValue = "${failIfExists}")
@Parameter(property = "exists.failIfExists", defaultValue = "false")
private boolean failIfExists;

/**
* Fail the build if the artifact does not exist in the repository.
*
* @since 0.0.3
*/
@Parameter(defaultValue = "${failIfNotExists}")
@Parameter(property = "exists.failIfNotExists", defaultValue = "false")
private boolean failIfNotExists;


/**
* Fail the build if the artifact checksum does not match the current repository artifact.
*
* @since 0.0.7
*/
@Parameter(defaultValue = "${failIfNotMatches}")
private boolean failIfNotMatches;
@Parameter(property = "exists.failIfNotMatch", defaultValue = "true")
private boolean failIfNotMatch;

/**
* Skip executing this plugin
*
* @since 0.0.4
*/
@Parameter(defaultValue = "false", property = "exists.skip")
@Parameter(property = "exists.skip", defaultValue = "false")
private boolean skip;

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

private static final Pattern GAV_PARSER = Pattern.compile("^([^:]*):([^:]*):([^:]*)$");

protected abstract byte[] getRemoteChecksum(String s) throws MojoExecutionException, IOException;
protected abstract String getRemoteChecksum(String s) throws Exception;

protected abstract String getRepositoryBase() throws MojoExecutionException;
protected abstract String getRepositoryBase() throws Exception;

protected abstract String getPropertyName();

Expand All @@ -111,19 +110,23 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}
boolean exists = verifyExistence();
if(!exists) {
getLog().debug("artifact does not exist");
return;
}

if(exists && useChecksum) {
exists = verifyChecksum();
if(useChecksum && verifyChecksum()) {
getLog().debug("checksum matches");
return;
}

String propertyName = getPropertyName();
String value = Boolean.toString(exists);
if (userProperty) {
getLog().info("setting user property " + propertyName + "=" + value);
session.getUserProperties().setProperty(propertyName, value);
getLog().info("setting user property " + propertyName + "=true");
session.getUserProperties().setProperty(propertyName, "true");
} else {
getLog().info("setting " + propertyName + "=" + value);
mavenProject.getProperties().setProperty(propertyName, value);
getLog().info("setting " + propertyName + "=true");
mavenProject.getProperties().setProperty(propertyName, "true");
}
} catch (MojoExecutionException|MojoFailureException e) {
throw e;
Expand All @@ -132,7 +135,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
}

private boolean verifyExistence() throws IOException, MojoExecutionException, MojoFailureException {
private boolean verifyExistence() throws Exception {
String uri = getRepositoryUri();
getLog().debug("checking for resource at " + uri);
boolean exists = checkArtifactExists(uri);
Expand All @@ -154,39 +157,32 @@ protected boolean isSnapshot() {
return project.endsWith("-SNAPSHOT");
}

protected abstract boolean checkArtifactExists(String uri) throws IOException, MojoExecutionException;
protected abstract boolean checkArtifactExists(String uri) throws Exception;

private boolean verifyChecksum()
throws IOException, MojoFailureException, NoSuchAlgorithmException, MojoExecutionException {
private boolean verifyChecksum() throws Exception {
String uri = getRepositoryUri();
getLog().debug("checking for resource " + uri);

String priorChecksum = getPriorChecksum(uri);
String buildChecksum = getArtifactChecksum();
if(buildChecksum.equalsIgnoreCase(priorChecksum)) {
if (buildChecksum.equalsIgnoreCase(priorChecksum)) {
return true;
}
String message = "buildChecksum(" + buildChecksum + ") != priorChecksum(" + priorChecksum + ")";
getLog().debug(message);
if (failIfNotMatches) {
getLog().info(message);
if (failIfNotMatch) {
throw new MojoFailureException(message);
}
return false;
}

private String getPriorChecksum(String uri) throws IOException {
try {
byte[] priorChecksumBytes = getRemoteChecksum(uri + ".sha1");
return new String(priorChecksumBytes, StandardCharsets.ISO_8859_1);
} catch (MojoExecutionException ex) {
getLog().debug("Unable to get prior checksum. Reason:" + ex.getMessage(), ex);
return null;
}
private String getPriorChecksum(String uri) throws Exception {
return getRemoteChecksum(uri);
}

// https://cwiki.apache.org/confluence/display/MAVEN/Remote+repository+layout

private String getRepositoryUri() throws MojoExecutionException {
private String getRepositoryUri() throws Exception {
Matcher matcher = GAV_PARSER.matcher(project);
if (!matcher.matches()) {
return getRepositoryBase() + '/' + project + '/' + artifact;
Expand All @@ -204,7 +200,7 @@ private String getRepositoryUri() throws MojoExecutionException {
+ artifact;
}

private String getArtifactChecksum() throws IOException, MojoFailureException, NoSuchAlgorithmException {
private String getArtifactChecksum() throws Exception {
CheckSum signer = new CheckSum("SHA-1");
Artifact mavenArtifact = mavenProject.getArtifact();
File file;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/honton/chas/exists/CheckSum.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ public CheckSum(String digestAlgorithm) throws NoSuchAlgorithmException {
digest = MessageDigest.getInstance(digestAlgorithm);
}

public String getChecksum(File file) throws IOException {
public byte[] getChecksumBytes(File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
try {
digest.reset();
readStream(fis);
return BaseEncoding.base16().encode(digest.digest());
return digest.digest();
} finally {
Closeables.closeQuietly(fis);
}
}
public String getChecksum(File file) throws IOException {
return BaseEncoding.base16().encode(getChecksumBytes(file));
}

private void readStream(FileInputStream fis) throws IOException {
FileChannel fileChannel = fis.getChannel();
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/org/honton/chas/exists/LocalExistsMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -24,7 +25,7 @@ public class LocalExistsMojo extends AbstractExistsMojo {
* The property to set if the artifact exists in the deploy repository.
* The default property of <em>maven.deploy.skip</em> may cause the deploy plugin to skip execution.
*/
@Parameter(defaultValue = "maven.install.skip")
@Parameter(property = "exists.property", defaultValue = "maven.install.skip")
private String property;

@Parameter( property = "localRepository", required = true, readonly = true )
Expand All @@ -41,16 +42,16 @@ protected String getRepositoryBase() {
}

@Override
protected boolean checkArtifactExists(String uri) throws IOException {
protected boolean checkArtifactExists(String uri) throws Exception {
return new File(uri).isFile();
}

@Override
protected byte[] getRemoteChecksum(String uri) throws MojoExecutionException, IOException {
File file = new File(uri);
if (!file.isFile()) {
return null;
protected String getRemoteChecksum(String uri) throws Exception {
File file = new File(uri + ".sha1");
if (file.isFile()) {
return new String(Files.readAllBytes(file.toPath()), StandardCharsets.ISO_8859_1);
}
return Files.readAllBytes(file.toPath());
return new CheckSum("SHA-1").getChecksum(new File(uri));
}
}
Loading

0 comments on commit 23d8cdc

Please sign in to comment.