Commit 730bc53f authored by Brian Long's avatar Brian Long
Browse files

added file-existence goal

parent 946052ec
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -7,3 +7,5 @@ pom.xml.versionsBackup
.project
.classpath

# Visual Studio Code
.factorypath
+124 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.inteligr8</groupId>
	<artifactId>conditional-maven-plugin-boolean-property</artifactId>
	<version>@pom.version@</version>
	<packaging>pom</packaging>

	<name>File Existence Plugin Tests</name>
	
	<properties>
		<file.exists>${basedir}/pom.xml</file.exists>
		<file.missing>${basedir}/pom2.xml</file.missing>
	</properties>
	
	<build>
		<plugins>
			<plugin>
				<groupId>${project.groupId}</groupId>
				<artifactId>conditional-maven-plugin</artifactId>
				<version>@pom.version@</version>
				<executions>
					<execution>
						<id>check-true-null</id>
						<phase>validate</phase>
						<goals>
							<goal>file-existence</goal>
						</goals>
						<configuration>
							<file>${file.exists}</file>
							<newProperty>prop.true-null</newProperty>
						</configuration>
					</execution>
					<execution>
						<id>check-true-empty</id>
						<phase>validate</phase>
						<goals>
							<goal>file-existence</goal>
						</goals>
						<configuration>
							<file>${file.exists}</file>
							<trueValue></trueValue>
							<newProperty>prop.true-empty</newProperty>
						</configuration>
					</execution>
					<execution>
						<id>check-true</id>
						<phase>validate</phase>
						<goals>
							<goal>file-existence</goal>
						</goals>
						<configuration>
							<file>${file.exists}</file>
							<trueValue>condition was true</trueValue>
							<newProperty>prop.true-valued</newProperty>
						</configuration>
					</execution>
					<execution>
						<id>check-false-null</id>
						<phase>validate</phase>
						<goals>
							<goal>file-existence</goal>
						</goals>
						<configuration>
							<file>${file.missing}</file>
							<newProperty>prop.false-null</newProperty>
						</configuration>
					</execution>
					<execution>
						<id>check-false-empty</id>
						<phase>validate</phase>
						<goals>
							<goal>file-existence</goal>
						</goals>
						<configuration>
							<file>${file.missing}</file>
							<falseValue></falseValue>
							<newProperty>prop.false-empty</newProperty>
						</configuration>
					</execution>
					<execution>
						<id>check-false</id>
						<phase>validate</phase>
						<goals>
							<goal>file-existence</goal>
						</goals>
						<configuration>
							<file>${file.missing}</file>
							<falseValue>condition was false</falseValue>
							<newProperty>prop.false-valued</newProperty>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<artifactId>maven-enforcer-plugin</artifactId>
				<version>1.4.1</version>
				<executions>
					<execution>
						<id>assert</id>
						<goals><goal>enforce</goal></goals>
						<configuration>
							<rules>
								<requireProperty>
									<property>prop.true-valued</property>
									<regex>condition was true</regex>
								</requireProperty>
								<requireProperty>
									<property>prop.false-valued</property>
									<regex>condition was false</regex>
								</requireProperty>
							</rules>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

</project>
+84 −0
Original line number Diff line number Diff line
/*
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.inteligr8.maven.conditional;

import java.io.File;

import javax.annotation.OverridingMethodsMustInvokeSuper;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;

import com.inteligr8.maven.ProjectPropertyResolver;

@Mojo( name = "file-existence", threadSafe = true )
@Component( role = org.apache.maven.plugin.Mojo.class )
public class FileExistenceMojo extends AbstractPropertyMojo {
	
	@Parameter( property = "file", required = true )
	protected String file;
	
	@Parameter( property = "emptyFileIsTrue" )
	protected boolean emptyFileIsTrue = false;
	
	@Requirement
	private ProjectPropertyResolver propResolver;

    @Override
    public void go() throws MojoExecutionException {
    	this.getLog().debug("Executing boolean property condition");

    	boolean value = false;
    	
    	File fileobj = new File(this.file);
    	if (fileobj.exists()) {
    		value = this.emptyFileIsTrue ? true : (fileobj.length() > 0L);
		}
		
    	this.executeOnCondition(value);
    }
    
    @Override
    @OverridingMethodsMustInvokeSuper
    protected void validateParamsPreNormalization() throws MojoFailureException {
    	super.validateParamsPreNormalization();
    	
    	if (this.file == null)
    		throw new MojoFailureException("The 'file' element is required");
    }

    @Override
    @OverridingMethodsMustInvokeSuper
    protected void normalizeParameters() throws MojoFailureException {
    	super.normalizeParameters();
    	
    	this.file = StringUtils.trimToNull(this.file);
    }
    
    @Override
    @OverridingMethodsMustInvokeSuper
    protected void validateParamsPostNormalization() throws MojoFailureException {
    	super.validateParamsPostNormalization();
    	
    	if (this.file == null)
    		throw new MojoFailureException("The 'file' element is required");
    }
    
}