Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ target/
.classpath
.settings/
.svn/
bin/
/bin/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???

Copy link
Member Author

@kwin kwin Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a bin folder is included in the test resources as this is part of the layout below maven home (https://github.com/apache/maven-verifier/pull/4/files#diff-770f62c0c49568f6bdd2aaabe732e084a54c6646756e823382668d9b888563cfR1)! The intent here is to only exclude compiled classes which will only ever end up in the to-level directory /bin (if at all)!

# Intellij
*.ipr
*.iml
Expand Down
86 changes: 56 additions & 30 deletions src/main/java/org/apache/maven/it/Verifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,32 @@ public class Verifier

private boolean debug;

/**
* If {@code true} uses {@link ForkedLauncher}, if {@code false} uses {@link Embedded3xLauncher},
* otherwise considers the value {@link #forkMode}.
*/
private Boolean forkJvm;

private String logFileName = LOG_FILENAME;

private String defaultMavenHome;

private String defaultClassworldConf;

private String defaultClasspath;
private String mavenHome;

// will launch mvn with --debug
private boolean mavenDebug = false;

/**
* Either "auto" (use {@link ForkedLauncher} when {@link #environmentVariables} is not empty,
* otherwise use {@link Embedded3xLauncher}) , "embedder" (always use {@link Embedded3xLauncher})
* or something else (always use {@link ForkedLauncher}).
* Set through system property {@code verifier.forkMode}.
* Only relevant if {@link #forkJvm} is {@code null}.
*/
private String forkMode;

private boolean debugJvm = false;

private boolean useWrapper;

private static MavenLauncher embeddedLauncher;

public Verifier( String basedir )
Expand Down Expand Up @@ -161,11 +170,23 @@ public Verifier( String basedir, String settingsFile, boolean debug, boolean for
public Verifier( String basedir, String settingsFile, boolean debug, boolean forkJvm, String[] defaultCliOptions )
throws VerificationException
{
this( basedir, settingsFile, debug, (Boolean) forkJvm, defaultCliOptions );
this( basedir, settingsFile, debug, forkJvm, defaultCliOptions, null );
}

public Verifier( String basedir, String settingsFile, boolean debug, String mavenHome )
throws VerificationException
{
this( basedir, settingsFile, debug, null, DEFAULT_CLI_OPTIONS, mavenHome );
}

private Verifier( String basedir, String settingsFile, boolean debug, Boolean forkJvm, String[] defaultCliOptions )
public Verifier( String basedir, String settingsFile, boolean debug, String mavenHome, String[] defaultCliOptions )
throws VerificationException
{
this( basedir, settingsFile, debug, null, defaultCliOptions, mavenHome );
}

private Verifier( String basedir, String settingsFile, boolean debug, Boolean forkJvm, String[] defaultCliOptions,
String mavenHome ) throws VerificationException
{
this.basedir = basedir;

Expand All @@ -182,22 +203,28 @@ private Verifier( String basedir, String settingsFile, boolean debug, Boolean fo
setDebug( debug );

findLocalRepo( settingsFile );
findDefaultMavenHome();
if ( mavenHome == null )
{
this.mavenHome = getDefaultMavenHome();
useWrapper = Files.exists( Paths.get( getBasedir(), "mvnw" ) );
}
else
{
this.mavenHome = mavenHome;
useWrapper = false;
}

if ( StringUtils.isEmpty( defaultMavenHome ) && StringUtils.isEmpty( forkMode ) )
if ( StringUtils.isEmpty( mavenHome ) && StringUtils.isEmpty( forkMode ) )
{
forkMode = "auto";
}

this.defaultCliOptions = defaultCliOptions == null ? new String[0] : defaultCliOptions.clone();
}

private void findDefaultMavenHome()
throws VerificationException
private static String getDefaultMavenHome()
{
defaultClasspath = System.getProperty( "maven.bootclasspath" );
defaultClassworldConf = System.getProperty( "classworlds.conf" );
defaultMavenHome = System.getProperty( "maven.home" );
String defaultMavenHome = System.getProperty( "maven.home" );

if ( defaultMavenHome == null )
{
Expand All @@ -213,6 +240,7 @@ private void findDefaultMavenHome()
defaultMavenHome = f.getAbsolutePath();
}
}
return defaultMavenHome;
}

public void setLocalRepo( String localRepo )
Expand Down Expand Up @@ -1250,8 +1278,6 @@ public String getExecutable()
// Use a strategy for finding the maven executable, John has a simple method like this
// but a little strategy + chain of command would be nicer.

String mavenHome = defaultMavenHome;

if ( mavenHome != null )
{
return mavenHome + "/bin/mvn";
Expand Down Expand Up @@ -1352,7 +1378,7 @@ public void executeGoals( List<String> goals, Map<String, String> envVars )
System.err.println( "Exit code: " + ret );

throw new VerificationException(
"Exit code was non-zero: " + ret + "; command line and log = \n" + new File( defaultMavenHome,
"Exit code was non-zero: " + ret + "; command line and log = \n" + new File( mavenHome,
"bin/mvn" ) + " "
+ StringUtils.join( args.iterator(), " " ) + "\n" + getLogContents( logFile ) );
}
Expand All @@ -1361,8 +1387,6 @@ public void executeGoals( List<String> goals, Map<String, String> envVars )
private MavenLauncher getMavenLauncher( Map<String, String> envVars )
throws LauncherException
{
boolean useWrapper = Files.exists( Paths.get( getBasedir(), "mvnw" ) );

boolean fork;
if ( useWrapper )
{
Expand Down Expand Up @@ -1404,7 +1428,7 @@ else if ( ( envVars.isEmpty() && "auto".equalsIgnoreCase( forkMode ) )
}
else
{
return new ForkedLauncher( defaultMavenHome, envVars, debugJvm, useWrapper );
return new ForkedLauncher( mavenHome, envVars, debugJvm, useWrapper );
}
}

Expand All @@ -1413,39 +1437,41 @@ private void initEmbeddedLauncher()
{
if ( embeddedLauncher == null )
{
if ( StringUtils.isEmpty( defaultMavenHome ) )
if ( StringUtils.isEmpty( mavenHome ) )
{
embeddedLauncher = Embedded3xLauncher.createFromClasspath();
}
else
{
embeddedLauncher =
Embedded3xLauncher.createFromMavenHome( defaultMavenHome, defaultClassworldConf, getClasspath() );
String defaultClasspath = System.getProperty( "maven.bootclasspath" );
String defaultClassworldConf = System.getProperty( "classworlds.conf" );
embeddedLauncher = Embedded3xLauncher.createFromMavenHome( mavenHome, defaultClassworldConf,
parseClasspath( defaultClasspath ) );
}
}
}

private List<URL> getClasspath()
private static List<URL> parseClasspath( String classpath )
throws LauncherException
{
if ( defaultClasspath == null )
if ( classpath == null )
{
return null;
}
ArrayList<URL> classpath = new ArrayList<URL>();
StringTokenizer st = new StringTokenizer( defaultClasspath, File.pathSeparator );
ArrayList<URL> classpathUrls = new ArrayList<URL>();
StringTokenizer st = new StringTokenizer( classpath, File.pathSeparator );
while ( st.hasMoreTokens() )
{
try
{
classpath.add( new File( st.nextToken() ).toURI().toURL() );
classpathUrls.add( new File( st.nextToken() ).toURI().toURL() );
}
catch ( MalformedURLException e )
{
throw new LauncherException( "Invalid launcher classpath " + defaultClasspath, e );
throw new LauncherException( "Invalid launcher classpath " + classpath, e );
}
}
return classpath;
return classpathUrls;
}

public String getMavenVersion()
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/apache/maven/it/ForkedLauncherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public void mvnwDebug() throws Exception

assertThat( "exit code", exitCode , is ( 0 ) );
}
private void expectFileLine( File file, String expectedline ) throws IOException

static void expectFileLine( File file, String expectedline ) throws IOException
{
try ( FileReader fr = new FileReader( file );
BufferedReader br = new BufferedReader( fr ) )
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/apache/maven/it/VerifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,26 @@
import static org.junit.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.isA;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.Arrays;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;

public class VerifierTest
{
@Rule
public final ExpectedException exception = ExpectedException.none();

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private void check( String expected, String... lines )
{
assertEquals( expected, ForkedLauncher.extractMavenVersion( Arrays.asList( lines ) ) );
Expand Down Expand Up @@ -95,4 +103,14 @@ public void testLoadPropertiesFNFE() throws VerificationException
verifier.loadProperties( "unknown.properties" );
}

@Test
public void testDedicatedMavenHome() throws VerificationException, IOException, URISyntaxException
{
String mavenHome = Paths.get( "src/test/resources/maven-home" ).toAbsolutePath().toString();;
Verifier verifier = new Verifier( temporaryFolder.getRoot().toString(), null, false, mavenHome );
verifier.executeGoal( "some-goal" );
File logFile = new File( verifier.getBasedir(), verifier.getLogFileName() );
ForkedLauncherTest.expectFileLine( logFile, "Hello World from Maven Home" );
}

}
20 changes: 20 additions & 0 deletions src/test/resources/maven-home/bin/mvn
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh
# ----------------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF 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.
# ----------------------------------------------------------------------------
echo Hello World from Maven Home
21 changes: 21 additions & 0 deletions src/test/resources/maven-home/bin/mvn.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@REM ----------------------------------------------------------------------------
@REM Licensed to the Apache Software Foundation (ASF) under one
@REM or more contributor license agreements. See the NOTICE file
@REM distributed with this work for additional information
@REM regarding copyright ownership. The ASF licenses this file
@REM to you under the Apache License, Version 2.0 (the
@REM "License"); you may not use this file except in compliance
@REM with the License. You may obtain a copy of the License at
@REM
@REM http://www.apache.org/licenses/LICENSE-2.0
@REM
@REM Unless required by applicable law or agreed to in writing,
@REM software distributed under the License is distributed on an
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@REM KIND, either express or implied. See the License for the
@REM specific language governing permissions and limitations
@REM under the License.
@REM ----------------------------------------------------------------------------
@echo off
@ECHO Hello World from Maven Home
@echo on