Skip to content

Commit

Permalink
Attempt at moving mvn as first class citizen in mvnd distribution, #392
Browse files Browse the repository at this point in the history
… (#769)

# Conflicts:
#	dist/src/main/provisio/maven-distro.xml
  • Loading branch information
gnodet committed Jan 24, 2023
1 parent 33422a3 commit 8d2ae6d
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private Process startDaemonProcess(String daemonId, ClientOutput output) {
final Path mvndHome = parameters.mvndHome();
final Path workingDir = parameters.userDir();
String command = "";
try (DirectoryStream<Path> jarPaths = Files.newDirectoryStream(mvndHome.resolve("mvn/lib/ext"))) {
try (DirectoryStream<Path> jarPaths = Files.newDirectoryStream(mvndHome.resolve("lib"))) {
List<String> args = new ArrayList<>();
// executable
final String java = Os.current().isUnixLike() ? "bin/java" : "bin\\java.exe";
Expand All @@ -358,10 +358,10 @@ private Process startDaemonProcess(String daemonId, ClientOutput output) {
}
}
if (mvndCommonPath == null) {
throw new IllegalStateException("Could not find mvnd-common jar in mvn/lib/ext/");
throw new IllegalStateException("Could not find mvnd-common jar in lib/");
}
if (mvndAgentPath == null) {
throw new IllegalStateException("Could not find mvnd-agent jar in mvn/lib/ext/");
throw new IllegalStateException("Could not find mvnd-agent jar in lib/");
}
args.add("-classpath");
args.add(mvndCommonPath + File.pathSeparator + mvndAgentPath);
Expand Down Expand Up @@ -419,8 +419,8 @@ private Process startDaemonProcess(String daemonId, ClientOutput output) {
}

Environment.MVND_HOME.addSystemProperty(args, mvndHome.toString());
args.add("-Dmaven.home=" + mvndHome.resolve("mvn"));
args.add("-Dmaven.conf=" + mvndHome.resolve("mvn/conf"));
args.add("-Dmaven.home=" + mvndHome);
args.add("-Dmaven.conf=" + mvndHome.resolve("conf"));

Environment.MVND_JAVA_HOME.addSystemProperty(
args, parameters.javaHome().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ private String mvndHomeFromExecutable() {
if (Environment.isNative() && cmd.isPresent()) {
final Path mvndH = Paths.get(cmd.get()).getParent().getParent();
if (mvndH != null) {
final Path mvndDaemonLib = mvndH.resolve("mvn/lib/ext/mvnd-daemon-"
+ BuildProperties.getInstance().getVersion() + ".jar");
final Path mvndDaemonLib = mvndH.resolve(
"lib/mvnd-daemon-" + BuildProperties.getInstance().getVersion() + ".jar");
if (Files.exists(mvndDaemonLib)) {
return mvndH.toString();
}
Expand Down Expand Up @@ -238,7 +238,8 @@ public Path multiModuleProjectDirectory(Path projectDir) {

public Path logbackConfigurationPath() {
return property(Environment.MVND_LOGBACK)
.orDefault(() -> mvndHome().resolve("conf/logback.xml").toString())
.orDefault(() ->
mvndHome().resolve("conf/logging/logback-mvnd.xml").toString())
.orFail()
.asPath();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void main(String[] args) throws Exception {
final Path mvndHome = Environment.MVND_HOME.asPath();
URL[] classpath = Stream.concat(
/* jars */
Stream.of("mvn/lib/ext", "mvn/lib", "mvn/boot")
Stream.of("lib/ext", "lib", "boot")
.map(mvndHome::resolve)
.flatMap((Path p) -> {
try {
Expand All @@ -46,7 +46,7 @@ public static void main(String[] args) throws Exception {
})
.filter(Files::isRegularFile),
/* resources */
Stream.of(mvndHome.resolve("mvn/conf"), mvndHome.resolve("mvn/conf/logging")))
Stream.of(mvndHome.resolve("conf"), mvndHome.resolve("conf/logging")))
.map(Path::normalize)
.map(Path::toUri)
.map(uri -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ exec "$JAVACMD" \
$MAVEN_DEBUG_OPTS \
-classpath "${CLASSWORLDS_JAR}" \
"-Dlogback.configurationFile=${MAVEN_HOME}/conf/logging/logback.xml" \
"-Dclassworlds.conf=${MAVEN_HOME}/bin/m2.conf" \
"-Dclassworlds.conf=${MAVEN_HOME}/bin/mvn.conf" \
"-Dmaven.home=${MAVEN_HOME}" \
"-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
${CLASSWORLDS_LAUNCHER} "$@"
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ set CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
%MAVEN_DEBUG_OPTS% ^
-classpath %CLASSWORLDS_JAR% ^
"-Dlogback.configurationFile=%MAVEN_HOME%\conf\logging\logback.xml" ^
"-Dclassworlds.conf=%MAVEN_HOME%\bin\m2.conf" ^
"-Dclassworlds.conf=%MAVEN_HOME%\bin\mvn.conf" ^
"-Dmaven.home=%MAVEN_HOME%" ^
"-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^
%CLASSWORLDS_LAUNCHER% %MAVEN_CMD_LINE_ARGS%
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions dist/src/main/distro/bin/mvnd.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do s

:endReadAdditionalConfig

for %%i in ("%MVND_HOME%"\mvn\boot\plexus-classworlds-*) do set CLASSWORLDS_JAR="%%i"
for %%i in ("%MVND_HOME%"\boot\plexus-classworlds-*) do set CLASSWORLDS_JAR="%%i"
set CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher

"%JAVACMD%" ^
%JVM_CONFIG_MAVEN_PROPS% ^
%MAVEN_OPTS% ^
%MAVEN_DEBUG_OPTS% ^
-classpath %CLASSWORLDS_JAR% ^
"-Dclassworlds.conf=%MVND_HOME%\bin\m2.conf"
"-Dclassworlds.conf=%MVND_HOME%\bin\mvnd.conf"
"-Dmvnd.home=%MVND_HOME%" ^
"-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^
%CLASSWORLDS_LAUNCHER% %MAVEN_CMD_LINE_ARGS%
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
# under the License.
main is org.mvndaemon.mvnd.client.DefaultClient from plexus.core

set maven.home default ${mvnd.home}/mvn
set maven.home default ${mvnd.home}
set maven.conf default ${maven.home}/conf
set logback.configurationFile default ${mvnd.home}/conf/logback-client.xml

set logback.configurationFile default ${maven.conf}/logging/logback-client.xml
set logback.configurationFile.fallback default ${maven.conf}/logging/logback.xml

[plexus.core]
Expand Down
4 changes: 2 additions & 2 deletions dist/src/main/distro/bin/mvnd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ if [ ! -x "$JAVACMD" ] ; then
exit 1
fi

CLASSWORLDS_JAR=`echo "${MVND_HOME}"/mvn/boot/plexus-classworlds-*.jar`
CLASSWORLDS_JAR=`echo "${MVND_HOME}"/boot/plexus-classworlds-*.jar`
CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher

# For Cygwin, switch paths to Windows format before running java
Expand Down Expand Up @@ -184,7 +184,7 @@ exec "$JAVACMD" \
$MAVEN_OPTS \
$MAVEN_DEBUG_OPTS \
-classpath "${CLASSWORLDS_JAR}" \
"-Dclassworlds.conf=${MVND_HOME}/bin/m2.conf" \
"-Dclassworlds.conf=${MVND_HOME}/bin/mvnd.conf" \
"-Dmvnd.home=${MVND_HOME}" \
"-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
${CLASSWORLDS_LAUNCHER} "$@"
File renamed without changes.
82 changes: 35 additions & 47 deletions dist/src/main/provisio/maven-distro.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,53 @@
-->
<assembly>

<artifactSet to="/mvn">
<artifactSet to="/">
<artifact id="org.apache.maven:apache-maven:tar.gz:bin">
<unpack useRoot="false"
excludes="lib/*slf4j*,conf/logging/*,lib/maven-slf4j-provider*,bin/mvn*,lib/jansi-*.jar,lib/jansi-native/*,lib/maven-resolver-api-*,lib/maven-resolver-impl-*,lib/maven-resolver-spi-*,lib/maven-resolver-util-*,lib/maven-resolver-connector-*,lib/maven-resolver-transport-*,lib/plexus-utils-*,lib/wagon-provider-api-*"/>
excludes="conf/logging/*,lib/maven-slf4j-provider*" />
</artifact>
</artifactSet>

<artifactSet to="/mvn/lib">
<exclusion id="javax.annotation:javax.annotation-api"/>
<artifact id="org.apache.maven.resolver:maven-resolver-api"/>
<artifact id="org.apache.maven.resolver:maven-resolver-impl"/>
<artifact id="org.apache.maven.resolver:maven-resolver-spi"/>
<artifact id="org.apache.maven.resolver:maven-resolver-util"/>
<artifact id="org.apache.maven.resolver:maven-resolver-connector-basic"/>
<artifact id="org.apache.maven.resolver:maven-resolver-transport-wagon"/>
<artifactSet to="/lib">
<artifact id="ch.qos.logback:logback-classic">
<exclusion id="*:*"/>
</artifact>
<artifact id="ch.qos.logback:logback-core">
<exclusion id="*:*"/>
</artifact>
</artifactSet>

<artifactSet to="/mvn/lib/ext">
<exclusion id="org.slf4j:slf4j-api"/>
<artifactSet to="/lib">
<artifact id="org.apache.maven.daemon:mvnd-daemon:${project.version}">
<exclusion id="org.codehaus.plexus:plexus-classworlds"/>
<exclusion id="*:cdi-api"/>
<exclusion id="*:commons-cli"/>
<exclusion id="*:commons-io"/>
<exclusion id="*:commons-lang3"/>
<exclusion id="*:guava"/>
<exclusion id="*:guice"/>
<exclusion id="*:javax.inject"/>
<exclusion id="*:jsr250-api"/>
<exclusion id="*:maven-artifact"/>
<exclusion id="*:maven-builder-support"/>
<exclusion id="*:maven-core"/>
<exclusion id="*:maven-embedder"/>
<exclusion id="*:maven-model"/>
<exclusion id="*:maven-model-builder"/>
<exclusion id="*:maven-plugin-api"/>
<exclusion id="*:maven-repository-metadata"/>
<exclusion id="*:maven-resolver-api"/>
<exclusion id="*:maven-resolver-impl"/>
<exclusion id="*:maven-resolver-provider"/>
<exclusion id="*:maven-resolver-spi"/>
<exclusion id="*:maven-resolver-util"/>
<exclusion id="*:maven-settings"/>
<exclusion id="*:maven-settings-builder"/>
<exclusion id="*:maven-shared-utils"/>
<exclusion id="*:org.eclipse.sisu.inject"/>
<exclusion id="*:org.eclipse.sisu.plexus"/>
<exclusion id="*:plexus-cipher"/>
<exclusion id="*:plexus-component-annotations"/>
<exclusion id="*:plexus-interpolation"/>
<exclusion id="*:plexus-sec-dispatcher"/>
<exclusion id="*:plexus-utils"/>
<exclusion id="*:plexus-container-default"/>
<exclusion id="*:*"/>
</artifact>
<artifact id="org.apache.maven.daemon:mvnd-client:${project.version}">
<exclusion id="*:*"/>
</artifact>
<artifact id="org.apache.maven.daemon:mvnd-agent:${project.version}"/>
<artifact id="org.apache.maven.daemon:mvnd-helper-agent:${project.version}"/>
<artifact id="org.apache.maven.daemon:mvnd-common:${project.version}">
<exclusion id="*:*"/>
</artifact>
<artifact id="org.apache.maven.daemon:mvnd-agent:${project.version}">
<exclusion id="*:*"/>
</artifact>
<artifact id="org.apache.maven.daemon:mvnd-helper-agent:${project.version}">
<exclusion id="*:*"/>
</artifact>
<artifact id="org.apache.maven.daemon:mvnd-native:${project.version}">
<exclusion id="*:*"/>
</artifact>
<artifact id="ch.qos.logback:logback-classic">
<exclusion id="*:*"/>
</artifact>
<artifact id="org.codehaus.plexus:plexus-interactivity-api">
<exclusion id="*:*"/>
</artifact>
<artifact id="org.jline:jline-terminal">
<exclusion id="*:*"/>
</artifact>
<artifact id="org.jline:jline-terminal-jansi">
<exclusion id="*:*"/>
</artifact>
</artifactSet>

<fileSet to="/">
Expand Down
56 changes: 26 additions & 30 deletions integration-tests/src/test/java/org/mvndaemon/mvnd/it/DistroIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand All @@ -33,27 +31,25 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class DistroIT {

/**
* Asserts that we do not have the same libs in lib/ext and in lib or boot directories.
*/
@Test
void noDuplicateJars() {
final Path mavenHome = Paths.get(System.getProperty("mvnd.home"));
Set<Avc> mavenLibs =
streamJars(mavenHome, "mvn/lib", "mvn/boot").collect(Collectors.toCollection(TreeSet::new));
Assertions.assertFalse(mavenLibs.isEmpty());
final List<Avc> mvndJars = streamJars(mavenHome, "mvn/lib/ext").collect(Collectors.toList());
Assertions.assertFalse(mvndJars.isEmpty());

final List<Avc> dups = mvndJars.stream()
.filter(avc -> mavenLibs.stream().anyMatch(mvnAvc -> mvnAvc.sameArtifactId(avc)))
String property = System.getProperty("mvnd.home");
assertNotNull(property, "mvnd.home must be defined");
final Path mavenHome = Paths.get(property);
List<Avc> avcs = listJars(mavenHome);
Map<String, List<Avc>> avcsByArtifactId = avcs.stream().collect(Collectors.groupingBy(Avc::getArtifactId));
List<List<Avc>> duplicateJars = avcsByArtifactId.values().stream()
.filter(list -> list.size() > 1)
.collect(Collectors.toList());

final String msg = mavenHome.resolve("mvn/lib/ext") + " contains duplicates available in "
+ mavenHome.resolve("mvn/lib") + " or " + mavenHome.resolve("mvn/boot");
Assertions.assertEquals(new ArrayList<Avc>(), dups, msg);
Assertions.assertTrue(duplicateJars.isEmpty(), mavenHome + " contains duplicates jars" + duplicateJars);
}

@Test
Expand All @@ -69,21 +65,17 @@ void assertAvcOf(String jarName, String artifactId, String version, String class
Assertions.assertEquals(classifier, avc.classifier, "classifier in " + jarName);
}

private static Stream<Avc> streamJars(Path mavenHome, String... dirs) {
return Stream.of(dirs)
.map(mavenHome::resolve)
.flatMap((Path p) -> {
try {
return Files.list(p);
} catch (java.io.IOException e) {
throw new RuntimeException("Could not list " + p, e);
}
})
.filter(p -> p.getFileName().toString().endsWith(".jar"))
.filter(Files::isRegularFile)
.map(Path::getFileName)
.map(Path::toString)
.map(Avc::of);
private static List<Avc> listJars(Path mavenHome) {
try (Stream<Path> stream = Files.walk(mavenHome)) {
return stream.filter(p -> p.getFileName().toString().endsWith(".jar"))
.filter(Files::isRegularFile)
.map(Path::getFileName)
.map(Path::toString)
.map(Avc::of)
.collect(Collectors.toList());
} catch (java.io.IOException e) {
throw new RuntimeException("Could not list " + mavenHome, e);
}
}

static class Avc implements Comparable<Avc> {
Expand Down Expand Up @@ -113,6 +105,10 @@ public Avc(String artifactId, String version, String classifier, String jarName)
this.jarName = jarName;
}

public String getArtifactId() {
return artifactId;
}

@Override
public String toString() {
return jarName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void version() throws IOException, InterruptedException {
"-DforceStdout",
"--raw-streams")
.assertSuccess();
String conf = parameters.mvndHome().resolve("mvn/conf").toString();
String conf = parameters.mvndHome().resolve("conf").toString();
assertTrue(
o.getMessages().stream().anyMatch(m -> m.toString().contains(conf)), "Output should contain " + conf);
}
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down

0 comments on commit 8d2ae6d

Please sign in to comment.