diff --git a/.gitignore b/.gitignore index a6f89c2..5bccb2c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/target/ \ No newline at end of file +/target/ +/src/main/resources/development.properties diff --git a/README.md b/README.md index 38c6e94..504c78d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,15 @@ # mavor -download a maven artifact and all its dependencies as zip \ No newline at end of file +download a maven artifact and all its dependencies as zip + +## Environment Variables +all environemnt variables are required +* `MAVOR_MAVEN_EXECUTABLE`: path to the maven executable, e.g. `/usr/bin/mvn` +* `MAVOR_TEMP_DIR`: path to a readable and writeable directory to temporarily store files, e.g. `/home/damage/Temp` + +## Development +To avoid setting environment variables during devleopment, create `src/main/resources/development.properties` with content like: +``` +MAVOR_MAVEN_EXECUTABLE=/usr/bin/mvn +MAVOR_TEMP_DIR=/home/damage/Temp +``` \ No newline at end of file diff --git a/src/main/java/de/devloop/mavor/Configuration.java b/src/main/java/de/devloop/mavor/Configuration.java index 03e6e26..1c7ccad 100644 --- a/src/main/java/de/devloop/mavor/Configuration.java +++ b/src/main/java/de/devloop/mavor/Configuration.java @@ -1,14 +1,36 @@ package de.devloop.mavor; +import java.io.IOException; +import java.io.InputStream; import java.util.Objects; +import java.util.Properties; public class Configuration { + private static final String DEVELOPMENT_PROPERTIES = "development.properties"; + private static final String ENV_MAVEN_EXECUTABLE = "MAVOR_MAVEN_EXECUTABLE"; private static final String ENV_TEMP_DIR = "MAVOR_TEMP_DIR"; - private final String mavenExecutable = Objects.requireNonNull(System.getProperty(ENV_MAVEN_EXECUTABLE)); - private final String tempDir = Objects.requireNonNull(System.getProperty(ENV_TEMP_DIR)); + private String mavenExecutable; + private String tempDir; + + public Configuration() throws IOException { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + InputStream developmentPropertiesStream = classLoader.getResourceAsStream(DEVELOPMENT_PROPERTIES); + if (Objects.nonNull(developmentPropertiesStream)) { + Properties developmentProperties = new Properties(); + developmentProperties.load(developmentPropertiesStream); + initByProperties(developmentProperties); + } else { + initByProperties(System.getProperties()); + } + } + + private void initByProperties(Properties properties) { + mavenExecutable = properties.getProperty(ENV_MAVEN_EXECUTABLE); + tempDir = properties.getProperty(ENV_TEMP_DIR); + } public String getMavenExecutable() { return mavenExecutable; @@ -17,4 +39,4 @@ public class Configuration { public String getTempDir() { return tempDir; } -} +} \ No newline at end of file diff --git a/src/main/java/de/devloop/mavor/servlet/DownloadJars.java b/src/main/java/de/devloop/mavor/servlet/DownloadJars.java index 2a87ded..d4c10b5 100644 --- a/src/main/java/de/devloop/mavor/servlet/DownloadJars.java +++ b/src/main/java/de/devloop/mavor/servlet/DownloadJars.java @@ -35,7 +35,16 @@ public class DownloadJars extends AuthenticatedServlet { private static final String PARAMETER_POM = "pom"; private static final String PARAMETER_TYPE = "type"; - private final Configuration configuration = new Configuration(); + private Configuration configuration; + + @Override + public void init() throws ServletException { + try { + configuration = new Configuration(); + } catch (IOException e) { + throw new ServletException("Configuration Error", e); + } + } @Override protected void doAuthenticatedPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { @@ -93,7 +102,7 @@ public class DownloadJars extends AuthenticatedServlet { } private ExecutionResult executeMaven(File tempDirJars) throws IOException, InterruptedException { - String mavenCmd = String.format("%s dependency:copy-dependencies -DoutputDirectory=\"%s\"", configuration.getMavenExecutable(), tempDirJars.getCanonicalPath()); + String mavenCmd = String.format("%s dependency:copy-dependencies -DoutputDirectory=%s", configuration.getMavenExecutable(), tempDirJars.getCanonicalPath()); Process mvnProcess = Runtime.getRuntime().exec(mavenCmd, null, tempDirJars.getCanonicalFile()); String stdout = new String(mvnProcess.getInputStream().readAllBytes(), StandardCharsets.UTF_8); String stderr = new String(mvnProcess.getErrorStream().readAllBytes(), StandardCharsets.UTF_8); diff --git a/src/main/java/de/devloop/mavor/servlet/DownloadZip.java b/src/main/java/de/devloop/mavor/servlet/DownloadZip.java index c8c83e7..20870fb 100644 --- a/src/main/java/de/devloop/mavor/servlet/DownloadZip.java +++ b/src/main/java/de/devloop/mavor/servlet/DownloadZip.java @@ -16,7 +16,16 @@ public class DownloadZip extends AuthenticatedServlet { private static final String PARAMETER_FILENAME = "file"; - private final Configuration configuration = new Configuration(); + private Configuration configuration; + + @Override + public void init() throws ServletException { + try { + configuration = new Configuration(); + } catch (IOException e) { + throw new ServletException("Configuration Error", e); + } + } @Override protected void doAuthenticatedGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {