Use muti-release jar to fallback mvnd-client to original maven (#722)

* Use muti-release jar to fallback mvnd-client to original maven

The mvnd-client is built to a muti-release jar. The default version of
    DefaultClient is compiled against the same target version as the
   embedded maven (4.x, so JDK 1.8) which only invoke the MavenCli.main(). 
The java-11 version is the full qualified mvnd-client.

* update cmd scripts

* embedded maven now works under JDK 1.8

Build SimpleAppender for JDK 1.8 since it is the log appender only
   for the embedded maven

Co-authored-by: Guillaume Nodet <gnodet@gmail.com>
This commit is contained in:
James Z.M. Gao
2022-12-15 00:41:02 +08:00
committed by GitHub
parent 64828123f5
commit f8d047bdae
13 changed files with 283 additions and 80 deletions

View File

@@ -31,8 +31,7 @@
<name>Maven Daemon - Client</name>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.release>11</maven.compiler.release>
<graalvm-native-static-opt/>
<graalvm-native-glibc-opt/>
</properties>
@@ -88,6 +87,7 @@
<configuration>
<rules>
<enforceBytecodeVersion>
<maxJdkVersion>${maven.compiler.release}</maxJdkVersion>
<excludes>
<exclude>org.graalvm.nativeimage:svm</exclude>
</excludes>
@@ -115,11 +115,48 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>fallback-main</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>${maven-dist.required.jdk}</release>
<compileSourceRoots>
<root>${project.basedir}/src/main/java-fallback</root>
</compileSourceRoots>
</configuration>
</execution>
<execution>
<id>mvnd-main</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<multiReleaseOutput>true</multiReleaseOutput>
<compileSourceRoots>
<root>${project.basedir}/src/main/java-mvnd</root>
</compileSourceRoots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>--add-exports=java.base/sun.nio.ch=ALL-UNNAMED</arg>
<arg>--add-exports=java.base/jdk.internal.module=ALL-UNNAMED</arg>
</compilerArgs>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed 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 org.mvndaemon.mvnd.client;
import org.apache.maven.cli.MavenCli;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DefaultClient {
public static void main(String[] argv) throws Exception {
final String logbackConfFallback = System.getProperty("logback.configurationFile.fallback");
if (null != logbackConfFallback && !"".equals(logbackConfFallback)) {
System.setProperty("logback.configurationFile", logbackConfFallback);
System.clearProperty("logback.configurationFile.fallback");
}
final Logger LOGGER = LoggerFactory.getLogger(DefaultClient.class);
LOGGER.warn("Found old JDK, fallback to the embedded maven!");
LOGGER.warn("Use JDK 11+ to run maven-mvnd client!");
MavenCli.main(argv);
}
}

View File

@@ -68,6 +68,8 @@ public class DefaultClient implements Client {
private final DaemonParameters parameters;
public static void main(String[] argv) throws Exception {
System.clearProperty("logback.configurationFile.fallback");
final List<String> args = new ArrayList<>(Arrays.asList(argv));
// Log file

View File

@@ -88,7 +88,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default</id>
<id>default-compile</id>
<goals>
<goal>compile</goal>
</goals>
@@ -143,7 +143,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default</id>
<id>default-compile</id>
<goals>
<goal>compile</goal>
</goals>

View File

@@ -99,6 +99,29 @@
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>fallback</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>${maven-dist.required.jdk}</release>
<compileSourceRoots>
<root>${project.basedir}/src/main/java-fallback</root>
</compileSourceRoots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>

View File

@@ -1,45 +0,0 @@
/*
* 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.
*/
package org.mvndaemon.mvnd.syncontext;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.eclipse.aether.internal.impl.synccontext.named.FileGAVNameMapper;
import org.eclipse.aether.internal.impl.synccontext.named.NameMapper;
import org.eclipse.aether.internal.impl.synccontext.named.NamedLockFactorySelectorSupport;
import org.eclipse.aether.named.NamedLockFactory;
import org.eclipse.aether.named.providers.FileLockNamedLockFactory;
import org.eclipse.sisu.Priority;
/**
* Mvnd selector implementation: it differs from
* {@link org.eclipse.aether.internal.impl.synccontext.named.SimpleNamedLockFactorySelector} only by default values.
*/
@Singleton
@Named
@Priority(10)
public final class DaemonNamedLockFactorySelector extends NamedLockFactorySelectorSupport {
@Inject
public DaemonNamedLockFactorySelector(
final Map<String, NamedLockFactory> factories, final Map<String, NameMapper> nameMappers) {
super(factories, FileLockNamedLockFactory.NAME, nameMappers, FileGAVNameMapper.NAME);
}
}

27
dist/src/main/distro/bin/m2.conf vendored Normal file
View File

@@ -0,0 +1,27 @@
# 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.
main is org.mvndaemon.mvnd.client.DefaultClient from plexus.core
set maven.home default ${mvnd.home}/mvn
set maven.conf default ${maven.home}/conf
set logback.configurationFile default ${mvnd.home}/conf/logback-client.xml
set logback.configurationFile.fallback default ${maven.conf}/logging/logback.xml
[plexus.core]
load ${maven.conf}/logging
load ${maven.home}/lib/ext/*.jar
load ${maven.home}/lib/*.jar

View File

@@ -73,22 +73,106 @@ goto error
set MAVEN_CMD_LINE_ARGS=%*
@setlocal EnableExtensions EnableDelayedExpansion
for %%i in ("%MVND_HOME%"\mvn\boot\*.jar "%MVND_HOME%"\mvn\lib\ext\*.jar "%MVND_HOME%"\mvn\lib\*.jar) do set DAEMON_JAR=!DAEMON_JAR!;%%i
@endlocal & set DAEMON_JAR="%DAEMON_JAR%"
@REM Find the project basedir, i.e., the directory that contains the directory ".mvn".
@REM Fallback to current working directory if not found.
set DAEMON_LAUNCHER=org.mvndaemon.mvnd.client.DefaultClient
set "EXEC_DIR=%CD%"
set "WDIR=%EXEC_DIR%"
@REM Look for the --file switch and start the search for the .mvn directory from the specified
@REM POM location, if supplied.
set FILE_ARG=
:arg_loop
if "%~1" == "-f" (
set "FILE_ARG=%~2"
shift
goto process_file_arg
)
if "%~1" == "--file" (
set "FILE_ARG=%~2"
shift
goto process_file_arg
)
@REM If none of the above, skip the argument
shift
if not "%~1" == "" (
goto arg_loop
) else (
goto findBaseDir
)
:process_file_arg
if "%FILE_ARG%" == "" (
goto findBaseDir
)
if not exist "%FILE_ARG%" (
echo POM file "%FILE_ARG%" specified the -f/--file command-line argument does not exist >&2
goto error
)
if exist "%FILE_ARG%\*" (
set "POM_DIR=%FILE_ARG%"
) else (
call :get_directory_from_file "%FILE_ARG%"
)
if not exist "%POM_DIR%" (
echo Directory "%POM_DIR%" extracted from the -f/--file command-line argument "%FILE_ARG%" does not exist >&2
goto error
)
set "WDIR=%POM_DIR%"
goto findBaseDir
:get_directory_from_file
set "POM_DIR=%~dp1"
:stripPomDir
if not "_%POM_DIR:~-1%"=="_\" goto pomDirStripped
set "POM_DIR=%POM_DIR:~0,-1%"
goto stripPomDir
:pomDirStripped
exit /b
:findBaseDir
cd /d "%WDIR%"
:findBaseDirLoop
if exist "%WDIR%\.mvn" goto baseDirFound
cd ..
IF "%WDIR%"=="%CD%" goto baseDirNotFound
set "WDIR=%CD%"
goto findBaseDirLoop
:baseDirFound
set "MAVEN_PROJECTBASEDIR=%WDIR%"
cd /d "%EXEC_DIR%"
goto endDetectBaseDir
:baseDirNotFound
if "_%EXEC_DIR:~-1%"=="_\" set "EXEC_DIR=%EXEC_DIR:~0,-1%"
set "MAVEN_PROJECTBASEDIR=%EXEC_DIR%"
cd /d "%EXEC_DIR%"
:endDetectBaseDir
set "jvmConfig=\.mvn\jvm.config"
if not exist "%MAVEN_PROJECTBASEDIR%%jvmConfig%" goto endReadAdditionalConfig
@setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
:endReadAdditionalConfig
for %%i in ("%MVND_HOME%"\mvn\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 %DAEMON_JAR% ^
"-Dlogback.configurationFile=%MVND_HOME%\conf\logback-client.xml" ^
-classpath %CLASSWORLDS_JAR% ^
"-Dclassworlds.conf=%MVND_HOME%\bin\m2.conf"
"-Dmvnd.home=%MVND_HOME%" ^
"-Dmaven.home=%MVND_HOME%\mvn" ^
"-Dlibrary.jansi.path=%MVND_HOME%\mvn\lib\jansi-native" ^
%DAEMON_LAUNCHER% %MAVEN_CMD_LINE_ARGS%
"-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^
%CLASSWORLDS_LAUNCHER% %MAVEN_CMD_LINE_ARGS%
if ERRORLEVEL 1 goto error
goto end

View File

@@ -101,9 +101,8 @@ if [ ! -x "$JAVACMD" ] ; then
exit 1
fi
DAEMON_JAR=`echo "${MVND_HOME}"/mvn/boot/*.jar "${MVND_HOME}"/mvn/lib/ext/*.jar "${MVND_HOME}"/mvn/lib/*.jar`
DAEMON_JAR=$(echo $DAEMON_JAR | sed -e 's/ /:/g')
DAEMON_LAUNCHER=org.mvndaemon.mvnd.client.DefaultClient
CLASSWORLDS_JAR=`echo "${MVND_HOME}"/mvn/boot/plexus-classworlds-*.jar`
CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
@@ -113,10 +112,27 @@ if $cygwin ; then
JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
[ -n "$CLASSPATH" ] &&
CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
[ -n "$DAEMON_JAR" ] &&
DAEMON_JAR=`cygpath --path --windows "$DAEMON_JAR"`
[ -n "$CLASSWORLDS_JAR" ] &&
CLASSWORLDS_JAR=`cygpath --path --windows "$CLASSWORLDS_JAR"`
fi
# traverses directory structure from process work directory to filesystem root
# first directory with .mvn subdirectory is considered project base directory
find_maven_basedir() {
(
basedir=`find_file_argument_basedir "$@"`
wdir="${basedir}"
while [ "$wdir" != '/' ] ; do
if [ -d "$wdir"/.mvn ] ; then
basedir=$wdir
break
fi
wdir=`cd "$wdir/.."; pwd`
done
echo "${basedir}"
)
}
find_file_argument_basedir() {
(
basedir=`pwd`
@@ -154,17 +170,21 @@ concat_lines() {
fi
}
# Provide a "standardized" way to retrieve the CLI args that will
# work with both Windows and non-Windows executions.
MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"
export MAVEN_CMD_LINE_ARGS
MAVEN_PROJECTBASEDIR=`find_maven_basedir "$@"`
MAVEN_OPTS="`concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config"` $MAVEN_OPTS"
# For Cygwin, switch project base directory path to Windows format before
# executing Maven otherwise this will cause Maven not to consider it.
if $cygwin ; then
[ -n "$MAVEN_PROJECTBASEDIR" ] &&
MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
fi
exec "$JAVACMD" \
$MAVEN_OPTS \
$MAVEN_DEBUG_OPTS \
-classpath "${DAEMON_JAR}" \
"-Dlogback.configurationFile=${MVND_HOME}/conf/logback-client.xml" \
-classpath "${CLASSWORLDS_JAR}" \
"-Dclassworlds.conf=${MVND_HOME}/bin/m2.conf" \
"-Dmvnd.home=${MVND_HOME}" \
"-Dmaven.home=${MVND_HOME}/mvn" \
"-Dlibrary.jansi.path=${MVND_HOME}/mvn/lib/jansi-native" \
${DAEMON_LAUNCHER} "$@"
"-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
${CLASSWORLDS_LAUNCHER} "$@"

View File

@@ -193,8 +193,6 @@ exec "$JAVACMD" \
-classpath "${CLASSWORLDS_JAR}" \
"-Dlogback.configurationFile=${MAVEN_HOME}/conf/logging/logback.xml" \
"-Dclassworlds.conf=${MAVEN_HOME}/bin/m2.conf" \
"-Dmvnd.home=${MAVEN_HOME}" \
"-Dmaven.home=${MAVEN_HOME}" \
"-Dlibrary.jansi.path=${MAVEN_HOME}/lib/jansi-native" \
"-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
${CLASSWORLDS_LAUNCHER} "$@"

View File

@@ -174,9 +174,7 @@ set CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
-classpath %CLASSWORLDS_JAR% ^
"-Dlogback.configurationFile=%MAVEN_HOME%\conf\logging\logback.xml" ^
"-Dclassworlds.conf=%MAVEN_HOME%\bin\m2.conf" ^
"-Dmvnd.home=%MAVEN_HOME%" ^
"-Dmaven.home=%MAVEN_HOME%" ^
"-Dlibrary.jansi.path=%MAVEN_HOME%\lib\jansi-native" ^
"-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^
%CLASSWORLDS_LAUNCHER% %MAVEN_CMD_LINE_ARGS%
if ERRORLEVEL 1 goto error

22
pom.xml
View File

@@ -66,6 +66,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven-dist.required.jdk>8</maven-dist.required.jdk>
<project.build.outputTimestamp>2022-11-19T15:16:03Z</project.build.outputTimestamp>
<trimStackTrace>false</trimStackTrace>
<project.github.repository>apache/maven-mvnd</project.github.repository>
@@ -476,6 +477,27 @@
<id>source-distribution</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<inherited>false</inherited>
<executions>
<execution>
<id>build-with-jdk11+</id>
<goals>
<goal>enforce</goal>
</goals>
<phase>validate</phase>
<configuration>
<rules>
<requireJavaVersion>
<version>[11,)</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>