Skip to content

Commit

Permalink
#2 - ignore check pom.properties existence
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed May 14, 2019
1 parent 9841f5d commit e906f9e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
38 changes: 26 additions & 12 deletions src/main/java/org/bsc/maven/plugin/libutils/DeployFolderMojo.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.bsc.maven.plugin.libutils;

import static java.lang.String.format;
import static org.bsc.maven.plugin.libutils.MojoUtils.getArtifactCoordinateFromPropsInJar;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -164,16 +166,19 @@ public class DeployFolderMojo extends AbstractDeployMojo implements Constants {
*
*/
@Parameter(property = "generatePom", defaultValue = "true")
//@MojoParameter(expression="${generatePom}",defaultValue="true")
private boolean generatePom;
/**
* Whether to deploy snapshots with a unique version or not.
*
* @parameter expression="${uniqueVersion}" default-value="true"
*/
//@MojoParameter(expression="${uniqueVersion}", defaultValue="true")
private boolean uniqueVersion;

/**
* issue #2 : skip check pom.properties inside jar
*/
@Parameter(property = "ignorePomProperties", defaultValue = "false")
private boolean ignorePomProperties;

private void updatePom() {

Expand Down Expand Up @@ -269,7 +274,7 @@ public void execute() throws MojoExecutionException {

final String protocol = deploymentRepository.getProtocol();

if ("".equals(protocol) || protocol == null) {
if ( protocol == null || "".equals(protocol) ) {
throw new MojoExecutionException("No transfer protocol found.");
}

Expand All @@ -281,7 +286,9 @@ public void execute() throws MojoExecutionException {
final java.util.Properties deployedFiles = new java.util.Properties();

if (checkFile.exists()) {
deployedFiles.load(new java.io.FileReader(checkFile));
try ( final java.io.Reader checkFileReader = new java.io.FileReader(checkFile) ) {
deployedFiles.load(checkFileReader);
}
} else {
outputFolder.mkdirs();

Expand All @@ -305,7 +312,9 @@ public void execute() throws MojoExecutionException {

deployedFiles.setProperty(f.getName(), f.getAbsolutePath());

deployedFiles.store(new java.io.FileWriter(checkFile), "artifact deployed");
try( java.io.Writer checkFileWriter = new java.io.FileWriter(checkFile)) {
deployedFiles.store( checkFileWriter, "artifact deployed");
}
}

}
Expand Down Expand Up @@ -423,18 +432,23 @@ private Artifact execute(File file, ArtifactRepository deploymentRepository) thr
final String packaging = name.substring(++index);

Artifact result = null;
boolean isMavenArtifact = false;

if( "jar".compareToIgnoreCase(packaging)==0 ) {
if( !ignorePomProperties && "jar".compareToIgnoreCase(packaging)==0 ) {

final java.util.jar.JarFile jarFile = new java.util.jar.JarFile( file );

result = MojoUtils.getArtifactCoordinateFromPropsInJar(jarFile, this::createBuildArtifact );
getLog().info( format("artifact [%s] is already a maven artifact!", result));

final Optional<Artifact> artifact =
getArtifactCoordinateFromPropsInJar(jarFile, this::createBuildArtifact );

if( artifact.isPresent() ) {
result = artifact.get();
getLog().info( format("artifact [%s] is already a maven artifact!", result));
isMavenArtifact = true;
}

}

final boolean isMavenArtifact = result != null;


if( !isMavenArtifact ) {
String candidateArtifactVersion = version;

Expand Down
15 changes: 8 additions & 7 deletions src/main/java/org/bsc/maven/plugin/libutils/MojoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.InputStream;
import java.io.Reader;
import java.util.Enumeration;
import java.util.Optional;
import java.util.function.Function;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -88,7 +89,7 @@ public static Model readModel( InputStream pomFile )
* @throws java.io.IOException
* @throws org.apache.maven.plugin.MojoExecutionException
*/
public static <T> T getArtifactCoordinateFromPropsInJar( JarFile jarFile,
public static <T> Optional<T> getArtifactCoordinateFromPropsInJar( JarFile jarFile,
Function<java.util.Properties,T> creator ) throws IOException, MojoExecutionException
{

Expand All @@ -100,7 +101,7 @@ public static <T> T getArtifactCoordinateFromPropsInJar( JarFile jarFile,
if( jarEntries!=null ) {
while ( jarEntries.hasMoreElements() )
{
JarEntry entry = jarEntries.nextElement();
final JarEntry entry = jarEntries.nextElement();

if ( POM_PROPERTIES.matcher( entry.getName() ).matches() )
{
Expand All @@ -125,13 +126,13 @@ public static <T> T getArtifactCoordinateFromPropsInJar( JarFile jarFile,
null // ArtifactHandler
);
*/
return creator.apply( props );
return Optional.ofNullable(creator.apply( props ));
}
}
}
}

return null;
return Optional.empty();
}

/**
Expand All @@ -143,7 +144,7 @@ public static <T> T getArtifactCoordinateFromPropsInJar( JarFile jarFile,
* @throws java.io.IOException
* @throws org.apache.maven.plugin.MojoExecutionException
*/
public static <T> T getArtifactCoordinateFromXmlInJar( JarFile jarFile,
public static <T> Optional<T> getArtifactCoordinateFromXmlInJar( JarFile jarFile,
Function<Model,T> creator ) throws IOException, MojoExecutionException
{

Expand Down Expand Up @@ -178,13 +179,13 @@ public static <T> T getArtifactCoordinateFromXmlInJar( JarFile jarFile,
null // ArtifactHandler
);
*/
return creator.apply( model );
return Optional.ofNullable(creator.apply( model ));

}
}
}
}

return null;
return Optional.empty();
}
}
9 changes: 6 additions & 3 deletions src/test/java/org/bsc/maven/plugin/libutils/MyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
*/
package org.bsc.maven.plugin.libutils;

import static org.bsc.maven.plugin.libutils.MojoUtils.getArtifactCoordinateFromPropsInJar;
import static org.hamcrest.core.Is.is;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.hamcrest.core.Is;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.IsNull;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -63,7 +66,7 @@ public void getArtifactCoordinateFromJar() throws Exception {
final java.io.File jar = new java.io.File("src/test/resources/log4j.jar");
final java.util.jar.JarFile jarFile = new java.util.jar.JarFile( jar );

final Artifact result = MojoUtils.getArtifactCoordinateFromPropsInJar(jarFile, (props) -> {
final Optional<Artifact> result = getArtifactCoordinateFromPropsInJar(jarFile, (props) -> {
final String scope = "";
final String classifier = "";

Expand All @@ -77,6 +80,6 @@ public void getArtifactCoordinateFromJar() throws Exception {
);
});

Assert.assertThat( result, IsNull.notNullValue());
Assert.assertThat( result.isPresent(), is(true));
}
}

0 comments on commit e906f9e

Please sign in to comment.