provide dependencies via system call of mvn
This commit is contained in:
parent
6f0a53d929
commit
0f11dfcacc
38
pom.xml
38
pom.xml
@ -90,43 +90,5 @@
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.11.0</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.eclipse.sisu/org.eclipse.sisu.plexus -->
|
||||
<dependency>
|
||||
<groupId>org.eclipse.sisu</groupId>
|
||||
<artifactId>org.eclipse.sisu.plexus</artifactId>
|
||||
<version>0.9.0.M3</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.maven/maven-embedder -->
|
||||
<dependency>
|
||||
<groupId>org.apache.maven</groupId>
|
||||
<artifactId>maven-embedder</artifactId>
|
||||
<version>3.9.9</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.maven/maven-slf4j-provider -->
|
||||
<dependency>
|
||||
<groupId>org.apache.maven</groupId>
|
||||
<artifactId>maven-slf4j-provider</artifactId>
|
||||
<version>3.9.9</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.maven/maven-core -->
|
||||
<dependency>
|
||||
<groupId>org.apache.maven</groupId>
|
||||
<artifactId>maven-core</artifactId>
|
||||
<version>3.9.9</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.maven/maven-compat -->
|
||||
<dependency>
|
||||
<groupId>org.apache.maven</groupId>
|
||||
<artifactId>maven-compat</artifactId>
|
||||
<version>3.9.9</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -1,11 +1,16 @@
|
||||
package de.devloop.mavor.servlet;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.apache.maven.cli.MavenCli;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import de.devloop.mavor.AuthenticatedServlet;
|
||||
import jakarta.servlet.RequestDispatcher;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.annotation.WebServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
@ -16,29 +21,99 @@ public class Download extends AuthenticatedServlet {
|
||||
|
||||
private static final String PARAMETER_SITE = "site";
|
||||
private static final String PARAMETER_GROUP_ID = "groupId";
|
||||
private static final String PARAMETER_ARTEFACT_ID = "artefactId";
|
||||
private static final String PARAMETER_ARTIFACT_ID = "artifactId";
|
||||
private static final String PARAMETER_VERSION = "version";
|
||||
private static final String PARAMETER_POM = "pom";
|
||||
private static final String PARAMETER_TYPE = "type";
|
||||
|
||||
private static final String MAVEN_CMD = "/usr/bin/mvn";
|
||||
private static final String TEMP_DIR = "/home/damage/Temp/mavor";
|
||||
|
||||
@Override
|
||||
protected void doAuthenticatedGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
String site = req.getParameter(PARAMETER_SITE);
|
||||
String groupId = req.getParameter(PARAMETER_GROUP_ID);
|
||||
String artifactId = req.getParameter(PARAMETER_ARTEFACT_ID);
|
||||
String version = req.getParameter(PARAMETER_VERSION);
|
||||
// implement a lot of checks:
|
||||
// NPE, required parameters not given
|
||||
|
||||
String artifact = String.format("%s:%s:%s", groupId, artifactId, version);
|
||||
String type = req.getParameter(PARAMETER_TYPE);
|
||||
if (type.equals("artifact")) {
|
||||
String site = req.getParameter(PARAMETER_SITE);
|
||||
String groupId = req.getParameter(PARAMETER_GROUP_ID);
|
||||
String artifactId = req.getParameter(PARAMETER_ARTIFACT_ID);
|
||||
String version = req.getParameter(PARAMETER_VERSION);
|
||||
generatePomXmlByArtifact(site, groupId, artifactId, version);
|
||||
} else if (type.equals("pom")) {
|
||||
writePomXml(req.getParameter(PARAMETER_POM));
|
||||
} else {
|
||||
throw new ServletException("Unknown download type: " + type);
|
||||
}
|
||||
|
||||
PrintStream out = new PrintStream(resp.getOutputStream());
|
||||
ExecutionResult executionResult;
|
||||
try {
|
||||
executionResult = executeMaven();
|
||||
} catch (InterruptedException e) {
|
||||
throw new ServletException("Running maven failed", e);
|
||||
}
|
||||
|
||||
MavenCli cli = new MavenCli();
|
||||
System.setProperty("maven.multiModuleProjectDirectory", "/home/damage/Temp");
|
||||
// TODO: does not work -> generate pom.xml in temp dir and use dependency:copy-dependencies?
|
||||
System.setProperty("maven.repo.remote", site);
|
||||
cli.doMain(new String[]{"dependency:copy", "-Dartifact=" + artifact, "-DoutputDirectory=/home/damage/Temp"}, "/home/damage/Temp", out, out);
|
||||
req.setAttribute("stdout", executionResult.stdout);
|
||||
req.setAttribute("stderr", executionResult.stderr);
|
||||
req.setAttribute("exitcode", executionResult.exitCode);
|
||||
|
||||
//RequestDispatcher view = req.getRequestDispatcher("/download.jsp");
|
||||
//view.forward(req, resp);
|
||||
RequestDispatcher view = req.getRequestDispatcher("/download.jsp");
|
||||
view.forward(req, resp);
|
||||
}
|
||||
}
|
||||
|
||||
private ExecutionResult executeMaven() throws IOException, InterruptedException {
|
||||
Process mvnProcess = Runtime.getRuntime().exec(MAVEN_CMD + " dependency:copy-dependencies -DoutputDirectory=" + TEMP_DIR, null, new File(TEMP_DIR));
|
||||
String stdout = new String(mvnProcess.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
|
||||
String stderr = new String(mvnProcess.getErrorStream().readAllBytes(), StandardCharsets.UTF_8);
|
||||
|
||||
ExecutionResult executionResult = new ExecutionResult();
|
||||
|
||||
executionResult.exitCode = mvnProcess.waitFor();
|
||||
executionResult.stdout = stdout;
|
||||
executionResult.stderr = stderr;
|
||||
return executionResult;
|
||||
}
|
||||
|
||||
private void generatePomXmlByArtifact(String site, String groupId, String artifactId, String version) throws IOException {
|
||||
String pomXml = getResourceFileAsString("pomstub.xml");
|
||||
pomXml = pomXml.replace("{site-id}", "foo");
|
||||
pomXml = pomXml.replace("{site-url}", site);
|
||||
pomXml = pomXml.replace("{group-id}", groupId);
|
||||
pomXml = pomXml.replace("{artifact-id}", artifactId);
|
||||
pomXml = pomXml.replace("{version}", version);
|
||||
writePomXml(pomXml);
|
||||
}
|
||||
|
||||
private void writePomXml(String pomXml) throws IOException {
|
||||
File pomXmlFile = new File(TEMP_DIR + "/pom.xml");
|
||||
pomXmlFile.delete();
|
||||
FileWriter fileWriter = new FileWriter(pomXmlFile);
|
||||
fileWriter.write(pomXml);
|
||||
fileWriter.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads given resource file as a string.
|
||||
*
|
||||
* @param fileName path to the resource file
|
||||
* @return the file's contents
|
||||
* @throws IOException if read fails for any reason
|
||||
*/
|
||||
static String getResourceFileAsString(String fileName) throws IOException {
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
try (InputStream is = classLoader.getResourceAsStream(fileName)) {
|
||||
if (is == null) return null;
|
||||
try (InputStreamReader isr = new InputStreamReader(is);
|
||||
BufferedReader reader = new BufferedReader(isr)) {
|
||||
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class ExecutionResult {
|
||||
public int exitCode;
|
||||
public String stdout;
|
||||
public String stderr;
|
||||
}
|
||||
}
|
23
src/main/resources/pomstub.xml
Normal file
23
src/main/resources/pomstub.xml
Normal file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>de.devloop</groupId>
|
||||
<artifactId>mavor-download</artifactId>
|
||||
<version>0.0.7</version>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>{site-id}</id>
|
||||
<url>{site-url}</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>{group-id}</groupId>
|
||||
<artifactId>{artifact-id}</artifactId>
|
||||
<version>{version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -2,7 +2,8 @@
|
||||
<%@ page isELIgnored="false" %>
|
||||
<html>
|
||||
<body style="background-color:black; color:white">
|
||||
Downloading ${foo}<br/>
|
||||
<a href="/mavor/logout">logout</a>
|
||||
Output:<br/>
|
||||
<pre>${stdout}</pre>
|
||||
<a href="/mavor/logout">logout</a> | <a href="/mavor">back</a>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -4,10 +4,16 @@
|
||||
<body style="background-color:black; color:white">
|
||||
<h2>Hello ${username}</h2>
|
||||
<form method="get" action="/mavor/download">
|
||||
<input type="hidden" name="type" value="artifact"/>
|
||||
Site: <input type="text" name="site" value="https://source.devloop.de/api/packages/damage/maven/" /><br/>
|
||||
Group ID: <input type="text" name="groupId" value="de.svenkubiak"/><br/>
|
||||
Artifact ID: <input type="text" name="artifactId" value="jpushover11"/><br/>
|
||||
Version: <input type="text" name="version" value="7.0.4"/><br/>
|
||||
Group ID: <input type="text" name="groupId" value="org.apache.activemq"/><br/>
|
||||
Artifact ID: <input type="text" name="artifactId" value="artemis-core-client"/><br/>
|
||||
Version: <input type="text" name="version" value="2.39.0"/><br/>
|
||||
<input type="submit"/>
|
||||
</form>
|
||||
<form method="get" action="/mavor/download">
|
||||
<input type="hidden" name="type" value="pom"/>
|
||||
POM: <textarea name="pom"></textarea><br/>
|
||||
<input type="submit"/>
|
||||
</form>
|
||||
<a href="/mavor/logout">logout</a>
|
||||
|
Loading…
x
Reference in New Issue
Block a user