Skip to content
Closed
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
5 changes: 5 additions & 0 deletions maven-core/src/main/java/org/apache/maven/DefaultMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.apache.maven.internal.aether.MavenChainedWorkspaceReader;
import org.apache.maven.lifecycle.internal.ExecutionEventCatapult;
import org.apache.maven.lifecycle.internal.LifecycleStarter;
import org.apache.maven.logging.internal.DefaultOutputCapabilities;
import org.apache.maven.model.Prerequisites;
import org.apache.maven.model.building.ModelProblem;
import org.apache.maven.model.building.Result;
Expand Down Expand Up @@ -93,6 +94,9 @@ public class DefaultMaven implements Maven {
@Inject
private RepositorySystemSessionFactory repositorySessionFactory;

@Inject
private DefaultOutputCapabilities outputCapabilities;

@Inject
@Named(GraphBuilder.HINT)
private GraphBuilder graphBuilder;
Expand All @@ -102,6 +106,7 @@ public MavenExecutionResult execute(MavenExecutionRequest request) {
MavenExecutionResult result;

try {
request.getData().put(DefaultOutputCapabilities.REQUEST_DATA_KEY, outputCapabilities.asMap());
result = doExecute(request);
} catch (OutOfMemoryError e) {
result = addExceptionToResult(new DefaultMavenExecutionResult(), e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.apache.maven.logging;

import java.nio.charset.Charset;
import java.util.Optional;

/**
* Describes the current destination of Maven logging, including plugin log messages.
* The information follows logging configuration and is shared by all projects in a build.
* It does not describe files or streams written directly by a plugin.
*
* <p>Plugins compiled against older Maven APIs can obtain the same information without
* referencing this interface through
* {@code session.getRequest().getData().get("maven.logging.outputCapabilities")}.
* The value is an immutable {@code Map<String, String>}. Its {@code destination}
* entry is always present and contains {@code CONSOLE}, {@code FILE}, {@code REDIRECTED},
* or {@code UNKNOWN}. Its {@code encoding} entry contains the canonical
* {@link Charset#name()} when known and is otherwise absent, independently of destination.
* An absent request-data entry means the service is unavailable (for example, on older
* Maven versions); a present map with {@code UNKNOWN} means detection is unavailable.
* Clients should ignore additional keys and treat unfamiliar destinations as unknown.
*
* <p>The map captures the logging configuration when
* {@link org.apache.maven.Maven#execute(org.apache.maven.execution.MavenExecutionRequest)}
* begins, before validation and lifecycle callbacks. The entry is absent before execution
* unless it remains from an earlier execution of the same request. Each execution replaces
* the entry with a fresh capture, including when the request is copied or reused.
* The map and its collection views remain unchanged through logging cleanup,
* reconfiguration, and subsequent invocations. A retained map captured while information
* is unknown remains unknown. Cleanup does not remove the entry from the request.
* Unlike the map, this component's getters describe the current logging configuration
* and may therefore differ from a retained request's map after reconfiguration or cleanup.
*/
public interface OutputCapabilities {
/** The known kind of logging destination. */
enum Destination {
/** Output is attached to a terminal, independently of whether color is enabled. */
CONSOLE,
/** Maven or its logging provider opened an explicit file destination. */
FILE,
/** Output is not attached to a terminal; its final destination is not known. */
REDIRECTED,
/** The logging destination could not be determined. */
UNKNOWN
}

/**
* Returns the known kind of logging destination.
* @return the destination, never {@code null}
*/
Destination getDestination();

/**
* Returns the charset selected for the logging output route, if known.
* Encoding may be known even when the destination is unknown. It does not imply
* support for terminal control sequences or guarantee that a font contains a glyph.
* @return the output charset, or an empty optional when it cannot be determined
*/
Optional<Charset> getEncoding();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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.apache.maven.logging.internal;

import javax.inject.Named;
import javax.inject.Singleton;

import java.nio.charset.Charset;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.apache.maven.logging.OutputCapabilities;

/**
* Internal bridge between logging setup and the read-only component exposed to plugins.
* Publication and restoration follow the logging lifecycle, not the build session.
*/
@Named
@Singleton
public class DefaultOutputCapabilities implements OutputCapabilities {
public static final String REQUEST_DATA_KEY = "maven.logging.outputCapabilities";

public static final OutputCapabilities UNKNOWN = snapshot(Destination.UNKNOWN, null);

private volatile Snapshot current = (Snapshot) UNKNOWN;

/** Internal access to an immutable description of the current logging configuration. */
public Map<String, String> asMap() {
return current.properties;
}

@Override
public Destination getDestination() {
return current.getDestination();
}

@Override
public Optional<Charset> getEncoding() {
return current.getEncoding();
}

/** Creates an immutable description for transport from logging setup to the container. */
public static OutputCapabilities snapshot(Destination destination, Charset encoding) {
return new Snapshot(Objects.requireNonNull(destination), encoding);
}

/**
* Installs the current output description and returns its logging-lifecycle cleanup.
* An obsolete cleanup must not overwrite a subsequently installed configuration.
*/
public synchronized AutoCloseable install(OutputCapabilities capabilities) {
Snapshot previous = current;
Snapshot installed = new Snapshot(
capabilities.getDestination(), capabilities.getEncoding().orElse(null));
current = installed;
return () -> {
synchronized (DefaultOutputCapabilities.this) {
if (current == installed) {
current = previous;
}
}
};
}

private static final class Snapshot implements OutputCapabilities {
private final Destination destination;
private final Optional<Charset> encoding;
private final Map<String, String> properties;

private Snapshot(Destination destination, Charset encoding) {
this.destination = destination;
this.encoding = Optional.ofNullable(encoding);
Map<String, String> values = new LinkedHashMap<>();
values.put("destination", destination.name());
if (encoding != null) {
values.put("encoding", encoding.name());
}
this.properties = Collections.unmodifiableMap(values);
}

@Override
public Destination getDestination() {
return destination;
}

@Override
public Optional<Charset> getEncoding() {
return encoding;
}
}
}
1 change: 1 addition & 0 deletions maven-core/src/main/resources/META-INF/maven/extension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ under the License.
<exportedPackage>org.apache.maven.execution.scope</exportedPackage>
<exportedPackage>org.apache.maven.graph</exportedPackage>
<exportedPackage>org.apache.maven.lifecycle</exportedPackage>
<exportedPackage>org.apache.maven.logging.*</exportedPackage>
<exportedPackage>org.apache.maven.model</exportedPackage>
<exportedPackage>org.apache.maven.monitor</exportedPackage>
<exportedPackage>org.apache.maven.plugin</exportedPackage>
Expand Down
Loading
Loading