com.marvelution.maven.components.dependency.resolver.methods.UserInputResolveMethod Maven / Gradle / Ivy
/*
* Licensed to Marvelution under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Marvelution 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 com.marvelution.maven.components.dependency.resolver.methods;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.maven.artifact.Artifact;
import org.codehaus.plexus.components.interactivity.Prompter;
import org.codehaus.plexus.components.interactivity.PrompterException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import com.marvelution.maven.components.dependency.resolver.environment.ResolveEnvironment;
import com.marvelution.maven.components.dependency.resolver.methods.exception.ResolveMethodException;
import com.marvelution.utils.io.FileUtils;
/**
* User Input {@link ResolveMethod}
*
* Note: If not using Plexus, the following must be set before executing any resolve
method:
*
* - the {@link Logger} implementation using method {@link #enableLogging(org.codehaus.plexus.logging.Logger)}
* - the {@link ArtifactFactory} using method {@link #setArtifactFactory(ArtifactFactory)}
* - the {@link Prompter} using method {@link #setPrompter(Prompter)}
*
*
* @author Mark Rekveld
*/
public class UserInputResolveMethod extends AbstractLogEnabled {
/**
* Version match pattern
*/
private static final Pattern VERSION_PATTERN = Pattern.compile("-[0-9]");
/**
* Plexus Prompter component
*/
private Prompter prompter;
/**
* Sets the {@link Prompter} to use
*
* @param prompter the {@link Prompter} implementation
*/
public void setPrompter(Prompter prompter) {
this.prompter = prompter;
}
/**
* {@inheritDoc}
*/
public Artifact resolve(final File library, final ResolveEnvironment environment) throws ResolveMethodException {
if (!environment.isInteractive()) {
return null;
}
String groupId = "", artifactId = "", version = "", type = "";
final String filename = FileUtils.removeExtension(library.getName());
type = FileUtils.extension(library.getName());
final Matcher mat = VERSION_PATTERN.matcher(filename);
if (mat.find()) {
artifactId = filename.substring(0, mat.start());
version = filename.substring(mat.end() - 1);
} else {
artifactId = FileUtils.removeExtension(library.getName());
}
try {
getLogger().info("Specify GAV for library: " + library.getName());
groupId = prompter.prompt("Define artifact groupId", groupId);
artifactId = prompter.prompt("Define artifact artifactId", artifactId);
version = prompter.prompt("Define artifact version", version);
type = prompter.prompt("Define artifact type", type);
return environment.getArtifactFactory().createArtifact(groupId, artifactId, version, null, type);
} catch (PrompterException e) {
throw new ResolveMethodException(e.getMessage(), e);
}
}
}