Skip to content

Commit

Permalink
CLIng
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Sep 27, 2024
1 parent 2c6846b commit 371ff31
Show file tree
Hide file tree
Showing 27 changed files with 4,068 additions and 5 deletions.
4 changes: 4 additions & 0 deletions apache-maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ under the License.
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-cli</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
Expand Down
3 changes: 2 additions & 1 deletion apache-maven/src/assembly/maven/bin/m2.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
main is org.apache.maven.cli.MavenCli from plexus.core
# main is org.apache.maven.cli.MavenCli from plexus.core
main is org.apache.maven.cling.MavenCling from plexus.core

set maven.conf default ${maven.home}/conf
set maven.installation.conf default ${maven.conf}
Expand Down
99 changes: 99 additions & 0 deletions maven-cli/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.maven</groupId>
<artifactId>maven</artifactId>
<version>4.0.0-beta-5-SNAPSHOT</version>
</parent>

<artifactId>maven-cli</artifactId>

<name>Maven CLI</name>
<description>Maven CLI component, with CLI and logging support.</description>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-classworlds</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.plexus</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-jline</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.apache.maven.cling.MavenCling</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
92 changes: 92 additions & 0 deletions maven-cli/src/main/java/org/apache/maven/cling/MavenCling.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.cling;

import java.io.IOException;

import org.apache.maven.cling.invoker.InvokerException;
import org.apache.maven.cling.invoker.ParserException;
import org.apache.maven.cling.invoker.local.LocalInvoker;
import org.apache.maven.cling.invoker.local.LocalParser;
import org.apache.maven.jline.MessageUtils;
import org.codehaus.plexus.classworlds.ClassWorld;

import static java.util.Objects.requireNonNull;

/**
* Maven CLI "new-gen".
*/
public class MavenCling {
static final String CORE_CLASS_REALM_ID = "plexus.core";

/**
* "Normal" Java entry point. Note: Maven uses ClassWorld Launcher and this entry point is NOT used under normal
* circumstances.
*/
public static void main(String[] args) throws IOException {
int exitCode = new MavenCling().run(args);
System.exit(exitCode);
}

/**
* ClassWorld Launcher "enhanced" entry point: returning exitCode and accepts Class World.
*/
public static int main(String[] args, ClassWorld world) throws IOException {
return new MavenCling(world).run(args);
}

private final ClassWorld classWorld;
private final boolean classWorldManaged;

/**
* Ctor that creates "managed" ClassWorld. This constructor is not used in "normal" circumstances.
*/
public MavenCling() {
this.classWorld =
new ClassWorld(CORE_CLASS_REALM_ID, Thread.currentThread().getContextClassLoader());
this.classWorldManaged = true;
}

/**
* Ctor used when running in ClassWorlds Launcher.
*/
public MavenCling(ClassWorld classWorld) {
this.classWorld = requireNonNull(classWorld);
this.classWorldManaged = false;
}

/**
* The main entry point.
*/
public int run(String[] args) throws IOException {
MessageUtils.systemInstall();
MessageUtils.registerShutdownHook();
try {
return new LocalInvoker(classWorld).invoke(new LocalParser().parse(args));
} catch (InvokerException | ParserException e) {
System.err.println(e.getMessage());
return 1;
} finally {
if (classWorldManaged) {
classWorld.close();
}
MessageUtils.systemUninstall();
}
}
}
95 changes: 95 additions & 0 deletions maven-cli/src/main/java/org/apache/maven/cling/MavenTool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.cling;

import javax.lang.model.SourceVersion;
import javax.tools.Tool;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;

import org.apache.maven.cling.invoker.InvokerException;
import org.apache.maven.cling.invoker.ParserException;
import org.apache.maven.cling.invoker.ParserRequest;
import org.apache.maven.cling.invoker.local.LocalInvoker;
import org.apache.maven.cling.invoker.local.LocalParser;
import org.apache.maven.jline.MessageUtils;
import org.codehaus.plexus.classworlds.ClassWorld;

/**
* Maven Tool.
*/
public class MavenTool implements Tool {
@Override
public String name() {
return "mvn";
}

@Override
public Set<SourceVersion> getSourceVersions() {
// basically "all"?
return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3, SourceVersion.latest()));
}

@Override
public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
PrintStream stderr = toPsOrDef(err, System.err);
try (ClassWorld classWorld = new ClassWorld(
MavenCling.CORE_CLASS_REALM_ID, Thread.currentThread().getContextClassLoader())) {
MessageUtils.systemInstall();
MessageUtils.registerShutdownHook();
try {
return new LocalInvoker(classWorld)
.invoke(new LocalParser()
.parse(ParserRequest.builder(arguments)
.in(in)
.out(toPs(out))
.err(toPs(err))
.build()));
} catch (InvokerException | ParserException e) {
e.printStackTrace(stderr);
return 1;
} finally {
MessageUtils.systemUninstall();
}
} catch (IOException e) {
e.printStackTrace(stderr);
return 2;
}
}

private PrintStream toPs(OutputStream outputStream) {
return toPsOrDef(outputStream, null);
}

private PrintStream toPsOrDef(OutputStream outputStream, PrintStream def) {
if (outputStream == null) {
return def;
}
if (outputStream instanceof PrintStream ps) {
return ps;
}
return new PrintStream(outputStream);
}
}
Loading

0 comments on commit 371ff31

Please sign in to comment.