This is an automated email from the ASF dual-hosted git repository. gnodet pushed a commit to branch mvnd-0.9.x in repository https://gitbox.apache.org/repos/asf/maven-mvnd.git
commit b81c8568a82725c9ffc20c3ea019ae3cb5616e40 Author: James Z.M. Gao <gaozhim...@360.cn> AuthorDate: Thu Dec 15 00:41:02 2022 +0800 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 <gno...@gmail.com> # Conflicts: # pom.xml --- client/pom.xml | 49 ++++++++-- .../org/mvndaemon/mvnd/client/DefaultClient.java | 37 ++++++++ .../org/mvndaemon/mvnd/client/DefaultClient.java | 2 + common/pom.xml | 4 +- daemon/pom.xml | 23 +++++ .../mvnd/logging/internal/SimpleAppender.java | 0 .../syncontext/DaemonNamedLockFactorySelector.java | 45 ---------- dist/src/main/distro/bin/m2.conf | 27 ++++++ dist/src/main/distro/bin/mvnd.cmd | 100 +++++++++++++++++++-- dist/src/main/distro/bin/mvnd.sh | 48 +++++++--- dist/src/main/distro/mvn/bin/mvn | 2 - dist/src/main/distro/mvn/bin/mvn.cmd | 2 - pom.xml | 24 ++++- 13 files changed, 283 insertions(+), 80 deletions(-) diff --git a/client/pom.xml b/client/pom.xml index 2c370fb2..0bf6ed9f 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -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> diff --git a/client/src/main/java-fallback/org/mvndaemon/mvnd/client/DefaultClient.java b/client/src/main/java-fallback/org/mvndaemon/mvnd/client/DefaultClient.java new file mode 100644 index 00000000..f50419aa --- /dev/null +++ b/client/src/main/java-fallback/org/mvndaemon/mvnd/client/DefaultClient.java @@ -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); + } +} diff --git a/client/src/main/java/org/mvndaemon/mvnd/client/DefaultClient.java b/client/src/main/java-mvnd/org/mvndaemon/mvnd/client/DefaultClient.java similarity index 99% rename from client/src/main/java/org/mvndaemon/mvnd/client/DefaultClient.java rename to client/src/main/java-mvnd/org/mvndaemon/mvnd/client/DefaultClient.java index 9c4774da..417f17b4 100644 --- a/client/src/main/java/org/mvndaemon/mvnd/client/DefaultClient.java +++ b/client/src/main/java-mvnd/org/mvndaemon/mvnd/client/DefaultClient.java @@ -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 diff --git a/common/pom.xml b/common/pom.xml index 9d040397..2aaf4a14 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -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> diff --git a/daemon/pom.xml b/daemon/pom.xml index cbf0700c..52d51b76 100644 --- a/daemon/pom.xml +++ b/daemon/pom.xml @@ -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> diff --git a/daemon/src/main/java/org/mvndaemon/mvnd/logging/internal/SimpleAppender.java b/daemon/src/main/java-fallback/org/mvndaemon/mvnd/logging/internal/SimpleAppender.java similarity index 100% rename from daemon/src/main/java/org/mvndaemon/mvnd/logging/internal/SimpleAppender.java rename to daemon/src/main/java-fallback/org/mvndaemon/mvnd/logging/internal/SimpleAppender.java diff --git a/daemon/src/main/java/org/mvndaemon/mvnd/syncontext/DaemonNamedLockFactorySelector.java b/daemon/src/main/java/org/mvndaemon/mvnd/syncontext/DaemonNamedLockFactorySelector.java deleted file mode 100644 index c6f73bac..00000000 --- a/daemon/src/main/java/org/mvndaemon/mvnd/syncontext/DaemonNamedLockFactorySelector.java +++ /dev/null @@ -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); - } -} diff --git a/dist/src/main/distro/bin/m2.conf b/dist/src/main/distro/bin/m2.conf new file mode 100644 index 00000000..1f7aa818 --- /dev/null +++ b/dist/src/main/distro/bin/m2.conf @@ -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 diff --git a/dist/src/main/distro/bin/mvnd.cmd b/dist/src/main/distro/bin/mvnd.cmd index 060d08e7..62d29d49 100644 --- a/dist/src/main/distro/bin/mvnd.cmd +++ b/dist/src/main/distro/bin/mvnd.cmd @@ -73,22 +73,106 @@ goto error set MAVEN_CMD_LINE_ARGS=%* +@REM Find the project basedir, i.e., the directory that contains the directory ".mvn". +@REM Fallback to current working directory if not found. + +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 %%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%" +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 -set DAEMON_LAUNCHER=org.mvndaemon.mvnd.client.DefaultClient +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 diff --git a/dist/src/main/distro/bin/mvnd.sh b/dist/src/main/distro/bin/mvnd.sh index 89352082..df821112 100755 --- a/dist/src/main/distro/bin/mvnd.sh +++ b/dist/src/main/distro/bin/mvnd.sh @@ -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} "$@" diff --git a/dist/src/main/distro/mvn/bin/mvn b/dist/src/main/distro/mvn/bin/mvn index f23e7bb7..97363a1e 100755 --- a/dist/src/main/distro/mvn/bin/mvn +++ b/dist/src/main/distro/mvn/bin/mvn @@ -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} "$@" diff --git a/dist/src/main/distro/mvn/bin/mvn.cmd b/dist/src/main/distro/mvn/bin/mvn.cmd index b08e9461..17cb1cb7 100644 --- a/dist/src/main/distro/mvn/bin/mvn.cmd +++ b/dist/src/main/distro/mvn/bin/mvn.cmd @@ -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 diff --git a/pom.xml b/pom.xml index 5689db3d..b9427503 100644 --- a/pom.xml +++ b/pom.xml @@ -66,7 +66,8 @@ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> - <project.build.outputTimestamp>2022-10-04T07:06:02Z</project.build.outputTimestamp> + <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> <repository.url>github.com:${project.github.repository}</repository.url> @@ -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>