forked from apache/maven-mvnd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use an agent to support processes launched with redirected io, fixes a…
- Loading branch information
Showing
13 changed files
with
460 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<!-- | ||
Copyright 2021 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. | ||
--> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.mvndaemon.mvnd</groupId> | ||
<artifactId>mvnd</artifactId> | ||
<version>0.2.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>mvnd-agent</artifactId> | ||
|
||
<packaging>jar</packaging> | ||
<name>Maven Daemon - Agent</name> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.javassist</groupId> | ||
<artifactId>javassist</artifactId> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<configuration> | ||
<archive> | ||
<manifestEntries> | ||
<Premain-Class>org.mvndaemon.mvnd.agent.Agent</Premain-Class> | ||
<Boot-Class-Path>mvnd-helper-agent-${project.version}.jar</Boot-Class-Path> | ||
</manifestEntries> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<filters> | ||
<filter> | ||
<artifact>org.javassist:javassist</artifact> | ||
<excludes> | ||
<exclude>META-INF/MANIFEST.MF</exclude> | ||
</excludes> | ||
</filter> | ||
</filters> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2021 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.agent; | ||
|
||
import java.lang.instrument.ClassFileTransformer; | ||
import java.lang.instrument.IllegalClassFormatException; | ||
import java.lang.instrument.Instrumentation; | ||
import java.security.ProtectionDomain; | ||
import javassist.ClassPool; | ||
import javassist.CtClass; | ||
|
||
public class Agent { | ||
|
||
public static final String START_WITH_PIPES = "if (redirects != null\n" | ||
+ " && redirects[1] == ProcessBuilder$Redirect.INHERIT\n" | ||
+ " && redirects[2] == ProcessBuilder$Redirect.INHERIT) {\n" | ||
+ " redirects[1] = redirects[2] = ProcessBuilder$Redirect.PIPE;" | ||
+ " Process p = start(redirects);\n" | ||
+ " AgentHelper.pump(p.getInputStream(), System.out);\n" | ||
+ " AgentHelper.pump(p.getErrorStream(), System.err);\n" | ||
+ " return p;\n" | ||
+ "}"; | ||
|
||
public static void premain(String args, Instrumentation instrumentation) throws Exception { | ||
instrumentation.addTransformer(new ClassFileTransformer() { | ||
@Override | ||
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, | ||
ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { | ||
if ("java/lang/ProcessBuilder".equals(className)) { | ||
try { | ||
ClassPool pool = ClassPool.getDefault(); | ||
CtClass clazz = pool.get("java.lang.ProcessBuilder"); | ||
pool.importPackage("org.mvndaemon.mvnd.pump"); | ||
clazz.getDeclaredMethod("start", | ||
new CtClass[] { clazz.getClassPool().get("java.lang.ProcessBuilder$Redirect[]") }) | ||
.insertBefore(START_WITH_PIPES); | ||
byte[] data = clazz.toBytecode(); | ||
clazz.detach(); | ||
return data; | ||
} catch (Throwable e) { | ||
System.err.println(e); | ||
throw new IllegalClassFormatException(e.toString()); | ||
} | ||
} else { | ||
return classfileBuffer; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<!-- | ||
Copyright 2021 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. | ||
--> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.mvndaemon.mvnd</groupId> | ||
<artifactId>mvnd</artifactId> | ||
<version>0.2.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>mvnd-helper-agent</artifactId> | ||
|
||
<packaging>jar</packaging> | ||
<name>Maven Daemon - Helper Agent</name> | ||
|
||
</project> |
29 changes: 29 additions & 0 deletions
29
helper/src/main/java/org/mvndaemon/mvnd/pump/AgentHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2021 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.pump; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintStream; | ||
|
||
public class AgentHelper { | ||
|
||
public static void pump(InputStream stream, PrintStream out) { | ||
new Thread(() -> new BufferedReader(new InputStreamReader(stream)).lines().forEach(out::println)).start(); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
integration-tests/src/test/java/org/mvndaemon/mvnd/it/JUnitPlatformTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2021 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.it; | ||
|
||
import javax.inject.Inject; | ||
import org.junit.jupiter.api.Test; | ||
import org.mvndaemon.mvnd.assertj.TestClientOutput; | ||
import org.mvndaemon.mvnd.client.Client; | ||
import org.mvndaemon.mvnd.client.DaemonParameters; | ||
import org.mvndaemon.mvnd.common.Message; | ||
import org.mvndaemon.mvnd.junit.MvndTest; | ||
|
||
import static junit.framework.Assert.assertTrue; | ||
|
||
@MvndTest(projectDir = "src/test/projects/junit-platform") | ||
public class JUnitPlatformTest { | ||
|
||
@Inject | ||
Client client; | ||
|
||
@Inject | ||
DaemonParameters parameters; | ||
|
||
@Test | ||
void cleanTestInheritIO() throws InterruptedException { | ||
|
||
final TestClientOutput output = new TestClientOutput(); | ||
client.execute(output, "clean", "test", "-e", "-Dmvnd.log.level=DEBUG").assertSuccess(); | ||
assertHasTestMessage(output); | ||
|
||
} | ||
|
||
private void assertHasTestMessage(final TestClientOutput output) { | ||
assertTrue(output.getMessages().stream() | ||
.filter(Message.ProjectEvent.class::isInstance) | ||
.map(Message.ProjectEvent.class::cast) | ||
.anyMatch(it -> it.getMessage().contains("[stdout] From test"))); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
integration-tests/src/test/projects/junit-platform/.mvn/maven.config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-Dmaven.wagon.httpconnectionManager.ttlSeconds=120 | ||
-Dmaven.wagon.http.retryHandler.requestSentEnabled=true | ||
-Dmaven.wagon.http.retryHandler.count=10 |
Oops, something went wrong.