Skip to content
Permalink
Browse files

MNG-5767 .mvn/ for project specific jvm options and maven parameters

Signed-off-by: Igor Fedorenko <ifedorenko@apache.org>
  • Loading branch information...
ifedorenko committed Jan 26, 2015
1 parent ee7dbab commit 8ed9a1caa8890773b45c6c408a4e40acf4f4b0fd
@@ -189,14 +189,39 @@ if $cygwin; then
CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
fi

# traverses directory structure from process work directory to filesystem root
# first directory with .mvn subdirectory is considered project base directory
find_maven_basedir() {
local basedir=$(pwd)
local wdir=$(pwd)
while [ "$wdir" != '/' ] ; do
wdir=$(cd $wdir/..; pwd)
if [ -d "$wdir"/.mvn ] ; then
basedir=$wdir
break
fi
done
echo "${basedir}"
}

# concatenates all lines of a file
concat_lines() {
if [ -f "$1" ]; then
echo "$(tr -s '\n' ' ' < "$1")"
fi
}

export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-$(find_maven_basedir)}
MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"

# Provide a "standardized" way to retrieve the CLI args that will
# work with both Windows and non-Windows executions.
MAVEN_CMD_LINE_ARGS="$@"
MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"
export MAVEN_CMD_LINE_ARGS

exec "$JAVACMD" \
$MAVEN_OPTS \
-classpath "${M2_HOME}"/boot/plexus-classworlds-*.jar \
"-Dclassworlds.conf=${M2_HOME}/bin/m2.conf" \
"-Dmaven.home=${M2_HOME}" \
"-Dmaven.home=${M2_HOME}" "-Dmaven.projectBasedir=${MAVEN_PROJECTBASEDIR}" \
${CLASSWORLDS_LAUNCHER} "$@"
@@ -93,6 +93,8 @@
// Request
// ----------------------------------------------------------------------------

private File projectBasedir;

private File basedir;

private List<String> goals;
@@ -1149,4 +1151,16 @@ public MavenExecutionRequest setToolchains( Map<String, List<ToolchainModel>> to
this.toolchains = toolchains;
return this;
}

@Override
public void setProjectBaseDirectory( File directory )
{
this.projectBasedir = directory;
}

@Override
public File getProjectBaseDirectory()
{
return projectBasedir;
}
}
@@ -415,4 +415,13 @@
*/
Map<String, List<ToolchainModel>> getToolchains();

/**
* @since 3.2.6
*/
void setProjectBaseDirectory( File file );

/**
* @since 3.2.6
*/
File getProjectBaseDirectory();
}
@@ -23,8 +23,10 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -92,6 +94,8 @@
import org.sonatype.plexus.components.sec.dispatcher.SecUtil;
import org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.google.inject.AbstractModule;

// TODO: push all common bits back to plexus cli and prepare for transition to Guice. We don't need 50 ways to make CLIs
@@ -106,6 +110,8 @@

public static final String THREADS_DEPRECATED = "maven.threads.experimental";

public static final String PROJECT_BASEDIR = "maven.projectBasedir";

@SuppressWarnings( "checkstyle:constantname" )
public static final String userHome = System.getProperty( "user.home" );

@@ -257,13 +263,27 @@ public int doMain( CliRequest cliRequest )
}
}

private void initialize( CliRequest cliRequest )
void initialize( CliRequest cliRequest )
{
if ( cliRequest.workingDirectory == null )
{
cliRequest.workingDirectory = System.getProperty( "user.dir" );
}

if ( cliRequest.projectBaseDirectory == null )
{
String basedirProperty = System.getProperty( PROJECT_BASEDIR );
File basedir = basedirProperty != null ? new File( basedirProperty ) : new File( "" );
try
{
cliRequest.projectBaseDirectory = basedir.getCanonicalFile();
}
catch ( IOException e )
{
cliRequest.projectBaseDirectory = basedir.getAbsoluteFile();
}
}

//
// Make sure the Maven home directory is an absolute path to save us from confusion with say drive-relative
// Windows paths.
@@ -276,7 +296,7 @@ private void initialize( CliRequest cliRequest )
}
}

private void cli( CliRequest cliRequest )
void cli( CliRequest cliRequest )
throws Exception
{
//
@@ -287,9 +307,38 @@ private void cli( CliRequest cliRequest )

CLIManager cliManager = new CLIManager();

List<String> args = new ArrayList<String>();

try
{
File configFile = new File( cliRequest.projectBaseDirectory, ".mvn/maven.config" );

if ( configFile.isFile() )
{
for ( String arg : Files.toString( configFile, Charsets.UTF_8 ).split( "\\s+" ) )
{
args.add( arg );
}

CommandLine config = cliManager.parse( args.toArray( new String[args.size()] ) );
List<?> unrecongized = config.getArgList();
if ( !unrecongized.isEmpty() )
{
throw new ParseException( "Unrecognized maven.config entries: " + unrecongized );
}
}
}
catch ( ParseException e )
{
System.err.println( "Unable to parse maven.config: " + e.getMessage() );
cliManager.displayHelp( System.out );
throw e;
}

try
{
cliRequest.commandLine = cliManager.parse( cliRequest.args );
args.addAll( 0, Arrays.asList( cliRequest.args ) );
cliRequest.commandLine = cliManager.parse( args.toArray( new String[args.size()] ) );
}
catch ( ParseException e )
{
@@ -1074,6 +1123,7 @@ else if ( request.isInteractiveMode() && !cliRequest.commandLine.hasOption( CLIM
.setUpdateSnapshots( updateSnapshots ) // default: false
.setNoSnapshotUpdates( noSnapshotUpdates ) // default: false
.setGlobalChecksumPolicy( globalChecksumPolicy ) // default: warn
.setProjectBaseDirectory( cliRequest.projectBaseDirectory )
;

if ( alternatePomFile != null )
@@ -1322,6 +1372,7 @@ private static void setCliProperty( String property, Properties properties )
CommandLine commandLine;
ClassWorld classWorld;
String workingDirectory;
File projectBaseDirectory;
boolean debug;
boolean quiet;
boolean showErrors = true;
@@ -19,16 +19,39 @@
* under the License.
*/

import java.io.File;

import junit.framework.TestCase;

import org.apache.commons.cli.ParseException;
import org.apache.maven.cli.MavenCli.CliRequest;

public class MavenCliTest
extends TestCase
{
private MavenCli cli;

private String origBasedir;

protected void setUp()
{
cli = new MavenCli();
origBasedir = System.getProperty( MavenCli.PROJECT_BASEDIR );
}

@Override
protected void tearDown()
throws Exception
{
if ( origBasedir != null )
{
System.setProperty( MavenCli.PROJECT_BASEDIR, origBasedir );
}
else
{
System.getProperties().remove( MavenCli.PROJECT_BASEDIR );
}
super.tearDown();
}

public void testCalculateDegreeOfConcurrencyWithCoreMultiplier()
@@ -49,4 +72,40 @@ public void testCalculateDegreeOfConcurrencyWithCoreMultiplier()
// carry on
}
}

public void testMavenConfig()
throws Exception
{
System.setProperty( MavenCli.PROJECT_BASEDIR, new File( "src/test/projects/config" ).getCanonicalPath() );
CliRequest request = new CliRequest( new String[0], null );

// read .mvn/maven.config
cli.initialize( request );
cli.cli( request );
assertEquals( "multithreaded", request.commandLine.getOptionValue( "builder" ) );
assertEquals( "8", request.commandLine.getOptionValue( "threads" ) );

// override from command line
request = new CliRequest( new String[] { "--builder", "foobar" }, null );
cli.cli( request );
assertEquals( "foobar", request.commandLine.getOptionValue( "builder" ) );
}

public void testMavenConfigInvalid()
throws Exception
{
System.setProperty( MavenCli.PROJECT_BASEDIR, new File( "src/test/projects/config-illegal" ).getCanonicalPath() );
CliRequest request = new CliRequest( new String[0], null );

cli.initialize( request );
try
{
cli.cli( request );
fail();
}
catch ( ParseException expected )
{

}
}
}
@@ -0,0 +1 @@
deploy
@@ -0,0 +1,2 @@
-T8 --builder
multithreaded

0 comments on commit 8ed9a1c

Please sign in to comment.
You can’t perform that action at this time.