Commit 478447b9 authored by Brian Long's avatar Brian Long
Browse files

upgrade to maven4

parent 98348c22
Loading
Loading
Loading
Loading
+56 −31
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"
<project xmlns="http://maven.apache.org/POM/4.1.0"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
		xsi:schemaLocation="http://maven.apache.org/POM/4.1.0 https://maven.apache.org/xsd/maven-4.1.0.xsd">
	<modelVersion>4.1.0</modelVersion>

	<groupId>com.inteligr8</groupId>
	<artifactId>regex-maven-plugin</artifactId>
	<version>1.0-SNAPSHOT</version>
	<version>2.0-SNAPSHOT</version>
	<packaging>maven-plugin</packaging>

	<name>Regular Expression Maven Plugin</name>
@@ -35,17 +35,19 @@
			<id>brian.long</id>
			<name>Brian Long</name>
			<email>brian@inteligr8.com</email>
			<url>https://twitter.com/brianmlong</url>
			<url>https://x.com/brianmlong</url>
		</developer>
	</developers>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.version>3.9.11</maven.version>
		<maven.compiler.source>17</maven.compiler.source>
		<maven.compiler.target>17</maven.compiler.target>
		<maven.compiler.release>17</maven.compiler.release>
		<maven.version>4.0.0-rc-5</maven.version>
	</properties>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.apache.commons</groupId>
@@ -59,38 +61,64 @@
			</dependency>
			<dependency>
				<groupId>org.apache.maven</groupId>
			<artifactId>maven-plugin-api</artifactId>
				<artifactId>maven-api-core</artifactId>
				<version>${maven.version}</version>
			<scope>provided</scope>
			</dependency>
			<dependency>
			<groupId>org.apache.maven.plugin-tools</groupId>
			<artifactId>maven-plugin-annotations</artifactId>
			<version>3.15.1</version>
			<scope>provided</scope>
				<groupId>org.apache.maven</groupId>
				<artifactId>maven-api-annotations</artifactId>
				<version>${maven.version}</version>
			</dependency>
			<dependency>
				<groupId>org.apache.maven.plugin-testing</groupId>
				<artifactId>maven-plugin-testing-harness</artifactId>
				<version>3.5.1</version>
			</dependency>
			<dependency>
				<groupId>org.apache.maven</groupId>
			<artifactId>maven-core</artifactId>
				<artifactId>maven-compat</artifactId>
				<version>${maven.version}</version>
			</dependency>
			<dependency>
				<groupId>org.junit.jupiter</groupId>
				<artifactId>junit-jupiter-api</artifactId>
				<version>6.0.3</version>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<dependencies>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.maven.shared</groupId>
			<artifactId>file-management</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-api-core</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-api-annotations</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.maven.plugin-testing</groupId>
			<artifactId>maven-plugin-testing-harness</artifactId>
			<version>3.3.0</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-compat</artifactId>
			<version>${maven.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.13.2</version>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
@@ -104,12 +132,7 @@
				</plugin>
				<plugin>
					<artifactId>maven-invoker-plugin</artifactId>
					<version>3.9.1</version>
				</plugin>
				<plugin>
					<groupId>org.codehaus.plexus</groupId>
					<artifactId>plexus-component-metadata</artifactId>
					<version>2.2.0</version>
					<version>3.10.1</version>
				</plugin>
			</plugins>
		</pluginManagement>
@@ -134,6 +157,7 @@
					</execution>
				</executions>
			</plugin>
			<!--
			<plugin>
				<groupId>org.codehaus.plexus</groupId>
				<artifactId>plexus-component-metadata</artifactId>
@@ -145,6 +169,7 @@
					</execution>
				</executions>
			</plugin>
			-->
			<plugin>
				<artifactId>maven-invoker-plugin</artifactId>
				<configuration>
+0 −27
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;

import java.util.Properties;

public interface ProjectPropertyResolver {
	
	Properties resolveScope(String propertyName);

	String resolve(String propertyName);
	
	String resolve(String propertyName, String defaultValue);

}
+0 −91
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;

import java.util.Properties;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Profile;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.AbstractLogEnabled;

@Component(role = ProjectPropertyResolver.class, instantiationStrategy = "per-lookup")
public class StandardProjectPropertyResolver extends AbstractLogEnabled implements ProjectPropertyResolver {
	
	@Requirement
	private MavenSession session;
	
	@Requirement
	private MavenProject project;
	
	@Override
	public String resolve(String propertyName) {
    	Properties props = this.findPropertiesObject(propertyName);
    	return props == null ? null : props.getProperty(propertyName);
    }
	
	@Override
	public String resolve(String propertyName, String defaultValue) {
    	Properties props = this.findPropertiesObject(propertyName);
    	return props == null ? null : props.getProperty(propertyName, defaultValue);
    }
	
	@Override
	public Properties resolveScope(String propertyName) {
		return this.findPropertiesObject(propertyName);
	}
    
    private Properties findPropertiesObject(String key) {
    	// search the user/cli properties first
    	Properties props = this.session.getUserProperties();
    	if (props.containsKey(key)) {
    	    this.getLogger().debug("Found in session user properties: " + key);
    		return props;
    	}
    	
    	MavenProject ancestor = this.project;
        while (ancestor != null) {
        	// search the profiles next; in order (FIXME maybe we should go backwards?)
        	for (Profile profile : ancestor.getActiveProfiles()) {
        		props = profile.getProperties();
        		if (props.containsKey(key)) {
                    this.getLogger().debug("Found in project profile properties: " + ancestor.getArtifact() + ": " + profile.getId() + ": " + key);
        			return props;
        		}
        	}
        	
        	// now look at the project props
        	props = ancestor.getProperties();
    		if (props.containsKey(key)) {
                this.getLogger().debug("Found in project properties: " + ancestor.getArtifact() + ": " + key);
    			return props;
    		}

            ancestor = ancestor.getParent();
        }

    	// search the system properties last (FIXME is this right?)
    	props = this.session.getSystemProperties();
    	if (props.containsKey(key)) {
            this.getLogger().debug("Found in system properties: " + key);
    		return props;
    	}
		
		return null;
    }

}
+17 −16
Original line number Diff line number Diff line
@@ -14,7 +14,6 @@
 */
package com.inteligr8.maven.regex;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
@@ -22,29 +21,31 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.api.di.Inject;
import org.apache.maven.api.plugin.Log;
import org.apache.maven.api.plugin.MojoException;
import org.apache.maven.api.plugin.annotations.Parameter;

public abstract class AbstractFileMojo extends AbstractReplaceMojo {
    
    @Inject
    private Log logger;
	
	@Parameter( property = "sourceDirectory", required = false )
	protected File sourceDirectory;
	protected Path sourceDirectory;

	@Parameter( property = "targetDirectory", required = false )
	protected File targetDirectory;
	protected Path targetDirectory;

	@Parameter( property = "overwrite", required = true, defaultValue = "true" )
	protected boolean overwrite = true;

    @Override
    public void go() throws MojoExecutionException {
    	this.getLog().debug("Executing file regex");
    public void go() throws MojoException {
    	this.logger.debug("Executing file regex");
    	
    	final Path sourcePath = this.sourceDirectory.toPath();
    	final Path targetPath = this.targetDirectory.toPath();
    	try {
	    	Files.walkFileTree(sourcePath, new FileVisitor<Path>() {
	    	Files.walkFileTree(this.sourceDirectory, new FileVisitor<Path>() {
	    		
	    		@Override
	    		public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
@@ -61,12 +62,12 @@ public abstract class AbstractFileMojo extends AbstractReplaceMojo {
	    		@Override
	    		public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
	    			if (!Files.isDirectory(file)) {
	    				String relativePath = sourcePath.relativize(file).toString();
	    				String relativePath = sourceDirectory.relativize(file).toString();
	    				String replacedRelativePath = replaceFirst(relativePath);
	    				if (!relativePath.equals(replacedRelativePath)) {
		    				Path newFile = targetPath.resolve(replacedRelativePath);
		    				Path newFile = targetDirectory.resolve(replacedRelativePath);
		    				if (file.equals(newFile)) {
		    					AbstractFileMojo.this.getLog().error("Relative paths are different, but the resultant paths are the same??");
		    					AbstractFileMojo.this.logger.error("Relative paths are different, but the resultant paths are the same??");
		    					throw new RuntimeException("This should never happen");
		    				}
		    				
@@ -83,14 +84,14 @@ public abstract class AbstractFileMojo extends AbstractReplaceMojo {
	    		}
			});
    	} catch (IOException ie) {
    		throw new MojoExecutionException(ie.getMessage(), ie);
    		throw new MojoException(ie.getMessage(), ie);
    	}
    }
    
    protected abstract void executeOnFile(Path sourcePath, Path targetPath) throws IOException;
    
    @Override
    protected void normalizeParameters() throws MojoFailureException {
    protected void normalizeParameters() throws MojoException {
    	super.normalizeParameters();
    	
    	if (this.sourceDirectory == null)
+24 −21
Original line number Diff line number Diff line
@@ -22,18 +22,22 @@ import java.nio.file.StandardOpenOption;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.api.di.Inject;
import org.apache.maven.api.plugin.Log;
import org.apache.maven.api.plugin.MojoException;
import org.apache.maven.api.plugin.annotations.Parameter;

import com.inteligr8.nio.DelimitedReadableByteChannel;
import com.inteligr8.maven.regex.nio.DelimitedReadableByteChannel;

public abstract class AbstractMatchMojo extends AbstractRegexMojo {
    
    @Inject
    private Log logger;

	@Parameter( property = "allowPartialMatch", required = true, defaultValue = "true" )
	protected boolean allowPartialMatch = true;
	
@@ -46,15 +50,14 @@ public abstract class AbstractMatchMojo extends AbstractRegexMojo {
	private List<Pattern> compiledPatterns;
	
	@Override
	protected void executeOnText(Properties props, String text, String newPropertyName) {
	protected void executeOnText(String text, String newPropertyName) {
		boolean matches = this.matches(text);
        if (this.verbose)
            this.getLog().info("Setting property: " + newPropertyName + ": " + matches);
            this.logger.info("Setting property: " + newPropertyName + ": " + matches);
        else if (matches)
        	this.getLog().info("Matches!");
		if (props == null)
			props = this.project.getProperties();
		props.setProperty(newPropertyName, String.valueOf(matches));
        	this.logger.info("Matches!");
        
        this.manager.setProperty(this.project, newPropertyName, String.valueOf(matches));
	}
	
	protected boolean matches(Path file, int chunkSize) throws IOException {
@@ -94,20 +97,20 @@ public abstract class AbstractMatchMojo extends AbstractRegexMojo {
			text = "";
		
    	for (Pattern pattern : this.compiledPatterns) {
        	this.getLog().debug("Applying regex pattern: " + pattern);
        	this.getLog().debug("Operating on value: " + text);
        	this.logger.debug("Applying regex pattern: " + pattern);
        	this.logger.debug("Operating on value: " + text);
        	
    		Matcher matcher = pattern.matcher(text);
    		if (this.allowPartialMatch) {
    			if (matcher.find()) {
                    if (this.verbose)
                        this.getLog().info("Pattern '" + pattern + "' matches text: " + text);
                        this.logger.info("Pattern '" + pattern + "' matches text: " + text);
    				return !this.negate;
    			}
    		} else {
    			if (matcher.matches()) {
                    if (this.verbose)
                        this.getLog().info("Pattern '" + pattern + "' matches text: " + text);
                        this.logger.info("Pattern '" + pattern + "' matches text: " + text);
    				return !this.negate;
    			}
    		}
@@ -117,15 +120,15 @@ public abstract class AbstractMatchMojo extends AbstractRegexMojo {
    }
	
	@Override
	protected void validateParamsPreNormalization() throws MojoFailureException {
	protected void validateParamsPreNormalization() throws MojoException {
		super.validateParamsPreNormalization();
    	
    	if (this.patterns == null)
    		throw new MojoFailureException("A 'patterns' element is required");
    		throw new MojoException("A 'patterns' element is required");
    }
	
	@Override
	protected void normalizeParameters() throws MojoFailureException {
	protected void normalizeParameters() throws MojoException {
		super.normalizeParameters();
    	
		ListIterator<String> p = this.patterns.listIterator();
@@ -138,19 +141,19 @@ public abstract class AbstractMatchMojo extends AbstractRegexMojo {
    }
	
	@Override
	protected void validateParamsPostNormalization() throws MojoFailureException {
	protected void validateParamsPostNormalization() throws MojoException {
		super.validateParamsPostNormalization();

		if (this.patterns.isEmpty())
			throw new MojoFailureException("At least one 'patterns' element is required");
			throw new MojoException("At least one 'patterns' element is required");
    	
    	this.compiledPatterns = new LinkedList<>();
    	for (String pattern : this.patterns) {
        	this.getLog().debug("Compiling regex pattern: " + pattern);
        	this.logger.debug("Compiling regex pattern: " + pattern);
        	try {
	    		this.compiledPatterns.add(Pattern.compile(pattern));
        	} catch (PatternSyntaxException pse) {
        		throw new MojoFailureException("'" + pattern + "' is not a valid regular expression: " + pse.getMessage());
        		throw new MojoException("'" + pattern + "' is not a valid regular expression: " + pse.getMessage());
        	}
    	}
    }
Loading