diff --git a/.gitignore b/.gitignore index 7e62d4fbd9..f9274f44a1 100644 --- a/.gitignore +++ b/.gitignore @@ -44,9 +44,7 @@ build/_offline build/camel-k-runtime-*-maven-offline.tar.gz build/_test build/_maven_output -build/_maven_overlay build/_kamelets -build/_maven_overlay/ build/maven/target/ build/maven build/m2 diff --git a/build/Dockerfile b/build/Dockerfile index f1523076cf..183fcc34a4 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -22,15 +22,12 @@ ARG MAVEN_HOME="/usr/share/maven" ARG MAVEN_DIST_URL="https://archive.apache.org/dist/maven/maven-3/${MAVEN_DEFAULT_VERSION}/binaries/apache-maven-${MAVEN_DEFAULT_VERSION}-bin.zip" ARG MVNW_DIR="/usr/share/maven/mvnw/" ARG MVN_REPO="/etc/maven/m2" -ARG MAVEN_OPTS="" USER 0 # Maven configuration RUN mkdir -p ${MAVEN_HOME} RUN mkdir -p ${MVN_REPO} -COPY build/_maven_overlay/ /usr/share/maven/lib/ -ADD build/_maven_overlay/logback.xml /usr/share/maven/conf/ # Prepare the maven wrapper required in the build Pod strategy COPY build/mvnw/mvnw.tar ${MVNW_DIR} @@ -39,13 +36,8 @@ RUN tar -xC ${MVNW_DIR} -f ${MVNW_DIR}mvnw.tar \ && sed -i "s;distributionUrl=.*;distributionUrl=${MAVEN_DIST_URL};" ${MVNW_DIR}.mvn/wrapper/maven-wrapper.properties # Used by mvnw to download maven dist into it ENV MAVEN_USER_HOME="${MAVEN_HOME}" -# Install a default mvnw distribution at build time and prepare the config for formatting log -RUN ${MVNW_DIR}/mvnw --version | grep "Maven home:" | sed 's/Maven home: //' >> ${MVNW_DIR}default \ - && cp -r /usr/share/maven/lib/. $(cat ${MVNW_DIR}default)/lib \ - && rm $(cat ${MVNW_DIR}default)/lib/maven-slf4j-provider* \ - && rm $(cat ${MVNW_DIR}default)/lib/slf4j-api-1.* - -ENV MAVEN_OPTS="${MAVEN_OPTS} -Dlogback.configurationFile=${MAVEN_HOME}/conf/logback.xml" +# Install a default mvnw distribution at build time +RUN ${MVNW_DIR}/mvnw --version ADD build/_maven_output ${MVN_REPO} # Fix https://github.com/moby/moby/issues/37965 diff --git a/e2e/support/test_support.go b/e2e/support/test_support.go index e314b378d4..8df8f8f686 100644 --- a/e2e/support/test_support.go +++ b/e2e/support/test_support.go @@ -496,7 +496,6 @@ func Logs(t *testing.T, ctx context.Context, ns, podName string, options corev1. } func StructuredLogs(t *testing.T, ctx context.Context, ns, podName string, options *corev1.PodLogOptions, ignoreParseErrors bool) ([]util.LogEntry, error) { - stream, err := TestClient(t).CoreV1().Pods(ns).GetLogs(podName, options).Stream(ctx) if err != nil { msg := "Error while reading container logs" diff --git a/java/.mvn/wrapper/maven-wrapper.jar b/java/.mvn/wrapper/maven-wrapper.jar deleted file mode 100644 index bf82ff01c6..0000000000 Binary files a/java/.mvn/wrapper/maven-wrapper.jar and /dev/null differ diff --git a/java/.mvn/wrapper/maven-wrapper.properties b/java/.mvn/wrapper/maven-wrapper.properties deleted file mode 100644 index 84b111fb23..0000000000 --- a/java/.mvn/wrapper/maven-wrapper.properties +++ /dev/null @@ -1,18 +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 -# -# https://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. -distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip -wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar diff --git a/java/maven-logging/pom.xml b/java/maven-logging/pom.xml deleted file mode 100644 index 2b0a3a2d91..0000000000 --- a/java/maven-logging/pom.xml +++ /dev/null @@ -1,116 +0,0 @@ - - - - - 4.0.0 - org.apache.camel.k - camel-k-maven-logging - 2.6.0-SNAPSHOT - Set of dependencies used for structural logging of Maven output - pom - - - 7.4 - 3.5.0 - 2.0.7 - 1.5.13 - 2.15.2 - - - - generate-resources - - - org.apache.maven.plugins - maven-dependency-plugin - ${maven-dependency-plugin-version} - - - default-cli - generate-resources - - copy-dependencies - - - true - ${project.build.directory}/maven-overlay - - - - - - - - - - - org.slf4j - slf4j-api - ${slf4j.version} - provided - - - ch.qos.logback - logback-core - ${logback.version} - provided - - - ch.qos.logback - logback-classic - ${logback.version} - provided - - - net.logstash.logback - logstash-logback-encoder - ${logstash-logback-encoder.version} - provided - - - com.fasterxml.jackson.core - jackson-annotations - - - com.fasterxml.jackson.core - jackson-core - - - - - com.fasterxml.jackson.core - jackson-annotations - ${jackson.version} - provided - - - com.fasterxml.jackson.core - jackson-core - ${jackson.version} - provided - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - provided - - - diff --git a/java/maven-logging/src/main/resources/logback.xml b/java/maven-logging/src/main/resources/logback.xml deleted file mode 100644 index f752b5011a..0000000000 --- a/java/maven-logging/src/main/resources/logback.xml +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - - - - level - - - ts - UTC - - - logger - - - msg - - - class - - - thread - - - - false - - - - stack - - 4096 - false - - - - - - - - - - - diff --git a/pkg/util/maven/maven_log.go b/pkg/util/maven/maven_log.go index a9d58d2fc2..8204c247ac 100644 --- a/pkg/util/maven/maven_log.go +++ b/pkg/util/maven/maven_log.go @@ -18,81 +18,68 @@ limitations under the License. package maven import ( - "encoding/json" - "strings" + "regexp" "github.com/apache/camel-k/v2/pkg/util/log" ) type mavenLog struct { - Level string `json:"level"` - TS string `json:"ts"` - Logger string `json:"logger"` - Msg string `json:"msg"` - Class string `json:"class"` - CallerMethodName string `json:"caller_method_name"` - CallerFileName string `json:"caller_file_name"` - CallerLineNumber int `json:"caller_line_number"` - Thread string `json:"thread"` + Level string `json:"level"` + Msg string `json:"msg"` } const ( - TRACE = "TRACE" - DEBUG = "DEBUG" - INFO = "INFO" - WARN = "WARN" - ERROR = "ERROR" - FATAL = "FATAL" + TRACE = "TRACE" + DEBUG = "DEBUG" + INFO = "INFO" + WARNING = "WARNING" + ERROR = "ERROR" + FATAL = "FATAL" ) var mavenLogger = log.WithName("maven.build") +var mavenLoggingFormat = regexp.MustCompile(`^\[(TRACE|DEBUG|INFO|WARNING|ERROR|FATAL)\] (.*)$`) +// LogHandler is in charge to log the text passed and, if the trace is an error, to return the message to the caller. func LogHandler(s string) string { - l, parseError := parseLog(s) - if parseError == nil { - normalizeLog(l) - } else { - // Why we are ignoring the parsing errors here: there are a few scenarios where this would likely occur. - // For example, if something outside of Maven outputs something (i.e.: the JDK, a misbehaved plugin, - // etc). The build may still have succeeded, though. - nonNormalizedLog(s) - } + l := parseLog(s) + normalizeLog(l) - // Return the error message according to maven log - if strings.HasPrefix(s, "[ERROR]") { - return s + if l.Level == ERROR { + return l.Msg } return "" } -func parseLog(line string) (mavenLog, error) { +func parseLog(line string) mavenLog { var l mavenLog - err := json.Unmarshal([]byte(line), &l) - if err != nil { - return l, err + matches := mavenLoggingFormat.FindAllStringSubmatch(line, -1) + if len(matches) == 0 || len(matches[0]) != 3 { + // If this is happening, then, we have a problem with parsing the maven output + // however we are printing the output in its plain format + l = mavenLog{ + Level: INFO, + Msg: line, + } + } else { + l = mavenLog{ + Level: matches[0][1], + Msg: matches[0][2], + } } - return l, nil + return l } func normalizeLog(mavenLog mavenLog) { switch mavenLog.Level { case DEBUG, TRACE: mavenLogger.Debug(mavenLog.Msg) - case INFO, WARN: + case INFO, WARNING: mavenLogger.Info(mavenLog.Msg) case ERROR, FATAL: mavenLogger.Error(nil, mavenLog.Msg) } } - -func nonNormalizedLog(rawLog string) { - // Distinguish an error message from the rest - if strings.HasPrefix(rawLog, "[ERROR]") { - mavenLogger.Error(nil, rawLog) - } else { - mavenLogger.Info(rawLog) - } -} diff --git a/pkg/util/maven/maven_log_test.go b/pkg/util/maven/maven_log_test.go index 46fadfdb00..dbd3307d75 100644 --- a/pkg/util/maven/maven_log_test.go +++ b/pkg/util/maven/maven_log_test.go @@ -24,6 +24,7 @@ import ( "testing" "github.com/apache/camel-k/v2/pkg/util" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -37,5 +38,23 @@ func TestRunAndLogErrorMvn(t *testing.T) { err := util.RunAndLog(context.Background(), cmd, LogHandler, LogHandler) require.Error(t, err) - require.ErrorContains(t, err, "[ERROR] The goal you specified requires a project to execute but there is no POM in this directory") + require.ErrorContains(t, err, "The goal you specified requires a project to execute but there is no POM in this directory") +} + +func TestParseLog(t *testing.T) { + mavenLogLine := parseLog("[INFO] this is an info log trace") + assert.Equal(t, INFO, mavenLogLine.Level) + assert.Equal(t, "this is an info log trace", mavenLogLine.Msg) +} + +func TestParseErrorLog(t *testing.T) { + mavenLogLine := parseLog("[ERROR] this is an error log trace") + assert.Equal(t, ERROR, mavenLogLine.Level) + assert.Equal(t, "this is an error log trace", mavenLogLine.Msg) +} + +func TestParseLogCannotTrace(t *testing.T) { + mavenLogLine := parseLog("[FAILING] this is a failing log trace") + assert.Equal(t, INFO, mavenLogLine.Level) + assert.Equal(t, "[FAILING] this is a failing log trace", mavenLogLine.Msg) } diff --git a/script/Makefile b/script/Makefile index 7c1cc637f5..83b44c734f 100644 --- a/script/Makefile +++ b/script/Makefile @@ -377,7 +377,6 @@ clean: rm -f *.test rm -rf $(LOCALBIN) rm -rf build/_maven_output - rm -rf build/_maven_overlay rm -rf build/_output rm -rf camel-k-client-*.tar.gz rm -rf camel-k-examples-*.tar.gz @@ -430,13 +429,6 @@ dir-licenses: check-licenses: ./script/check_licenses.sh -# The below are dependencies needed for maven structured logs. We must bundle into the final container image. -maven-overlay: - @echo "####### Preparing maven dependencies bundle..." - rm -rf build/_maven_overlay - mkdir -p build/_maven_overlay - ./script/maven_overlay.sh build/_maven_overlay - TARGET_STAGE := base ifeq ($(DEBUG_MODE),true) TARGET_STAGE := debug @@ -445,7 +437,7 @@ endif DOCKER_TAG := $(CUSTOM_IMAGE):$(CUSTOM_VERSION)-$(IMAGE_ARCH) -images: build maven-overlay image-build build-kamel-platform +images: build image-build build-kamel-platform image-build: ifneq (,$(findstring SNAPSHOT,$(DEFAULT_RUNTIME_VERSION))) diff --git a/script/maven_overlay.sh b/script/maven_overlay.sh deleted file mode 100755 index 3c02bcbac7..0000000000 --- a/script/maven_overlay.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash - -# 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. - -location=$(dirname $0) -rootdir=$(realpath ${location}/../) - -if [ "$#" -lt 1 ]; then - echo "usage: $0 " - exit 1 -fi - -options="" -if [ "$CI" = "true" ]; then - options="--batch-mode" -fi - -output_dir=$1 - -mvn -q $options -f java/maven-logging/pom.xml clean package -cp java/maven-logging/target/maven-overlay/*.jar ${output_dir} -cp java/maven-logging/src/main/resources/logback.xml ${output_dir}