From ed93f3e501e4db90092be3a8d5508317b747f892 Mon Sep 17 00:00:00 2001 From: Peter Palaga Date: Wed, 1 Jun 2022 20:43:58 +0200 Subject: [PATCH] basePath configuration parameter --- .../src/it/custom-base-path/pom.xml | 58 ++++++++++++++++ .../src/it/resolve/invoker.properties | 1 + .../custom-base-path/src/it/resolve/pom.xml | 68 +++++++++++++++++++ .../plugin/it/resolve/CustomBasePathTest.java | 44 ++++++++++++ .../src/it/resolve/verify.groovy | 4 ++ .../it/custom-base-path/src/it/settings.xml | 62 +++++++++++++++++ mrm-maven-plugin/src/it/hostedrepo/pom.xml | 2 +- .../local-maven-repo-proxy/invoker.properties | 1 + .../src/it/local-maven-repo-proxy/pom.xml | 18 +++++ .../it/local-maven-repo-proxy/verify.groovy | 4 ++ .../mojo/mrm/plugin/AbstractStartMojo.java | 19 +++++- .../mojo/mrm/plugin/FileSystemServer.java | 47 ++++++++++--- 12 files changed, 315 insertions(+), 13 deletions(-) create mode 100644 mrm-maven-plugin/src/it/custom-base-path/pom.xml create mode 100644 mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/invoker.properties create mode 100644 mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/pom.xml create mode 100644 mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/src/test/java/org/codehaus/mojo/mrm/plugin/it/resolve/CustomBasePathTest.java create mode 100644 mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/verify.groovy create mode 100644 mrm-maven-plugin/src/it/custom-base-path/src/it/settings.xml create mode 100644 mrm-maven-plugin/src/it/hostedrepo/src/it/local-maven-repo-proxy/invoker.properties create mode 100644 mrm-maven-plugin/src/it/hostedrepo/src/it/local-maven-repo-proxy/pom.xml create mode 100644 mrm-maven-plugin/src/it/hostedrepo/src/it/local-maven-repo-proxy/verify.groovy diff --git a/mrm-maven-plugin/src/it/custom-base-path/pom.xml b/mrm-maven-plugin/src/it/custom-base-path/pom.xml new file mode 100644 index 00000000..d949717c --- /dev/null +++ b/mrm-maven-plugin/src/it/custom-base-path/pom.xml @@ -0,0 +1,58 @@ + + + + 4.0.0 + + org.mojohaus.mrm.its + custom-base-path + 1.0.0-SNAPSHOT + pom + + + + + org.apache.maven.plugins + maven-invoker-plugin + @maven-invoker-plugin.version@ + + + + integration-test + verify + + + + + ${project.build.directory}/local-repo-it + src/it + ${project.build.directory}/it + src/it/settings.xml + + ${mrm.distribution.url} + ${mrm.repository.url} + + verify + true + verify + + + + org.codehaus.mojo + mrm-maven-plugin + @project.version@ + + + repository + + start + stop + + + /foo/bar + + + + + + + diff --git a/mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/invoker.properties b/mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/invoker.properties new file mode 100644 index 00000000..fd18ebcc --- /dev/null +++ b/mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/invoker.properties @@ -0,0 +1 @@ +invoker.goals = test diff --git a/mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/pom.xml b/mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/pom.xml new file mode 100644 index 00000000..4c3c338d --- /dev/null +++ b/mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/pom.xml @@ -0,0 +1,68 @@ + + + + 4.0.0 + + org.mojohaus.mrm.hostedrepo.its + mrm-resolve + 1.0.0 + jar + + + UTF-8 + 1.8 + ${maven.compiler.source} + + + + + + org.junit + junit-bom + 5.8.2 + pom + import + + + + + + + io.rest-assured + rest-assured + 5.1.0 + test + + + org.assertj + assertj-core + 3.22.0 + test + + + org.junit.jupiter + junit-jupiter + test + + + + + + + maven-compiler-plugin + 3.8.1 + + + maven-surefire-plugin + 2.22.2 + + + @mrm.repository.url@ + + + + + + + diff --git a/mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/src/test/java/org/codehaus/mojo/mrm/plugin/it/resolve/CustomBasePathTest.java b/mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/src/test/java/org/codehaus/mojo/mrm/plugin/it/resolve/CustomBasePathTest.java new file mode 100644 index 00000000..dfc93d34 --- /dev/null +++ b/mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/src/test/java/org/codehaus/mojo/mrm/plugin/it/resolve/CustomBasePathTest.java @@ -0,0 +1,44 @@ +package org.codehaus.mojo.mrm.plugin.it.resolve; + +/* + * 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. + */ + +import org.assertj.core.api.Assertions; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; + +import io.restassured.RestAssured; + +public class CustomBasePathTest { + + @Test + void customBasePath() { + final String mrmUri = System.getProperty("mrm.repository.url"); + + /* Make sure the repo ends with the base path we have set in mrm-maven-plugin/src/it/custom-base-path/pom.xml */ + Assertions.assertThat(mrmUri).endsWith("foo/bar"); + + /* Try to download something and make sure the content is as expected */ + RestAssured + .get(mrmUri + "/org/apache/commons/commons-lang3/3.12.0/commons-lang3-3.12.0.pom") + .then() + .statusCode(200) + .body(Matchers.containsString("commons-lang3"), Matchers.containsString("3.12.0")); + } +} diff --git a/mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/verify.groovy b/mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/verify.groovy new file mode 100644 index 00000000..dac6cdfd --- /dev/null +++ b/mrm-maven-plugin/src/it/custom-base-path/src/it/resolve/verify.groovy @@ -0,0 +1,4 @@ +// mrm-resolve test project depends on assertj, so it should appear in the test's local repo +assert new File( basedir, '../../local-repo-it/org/assertj/assertj-core/3.22.0/assertj-core-3.22.0.pom').exists() +// We have downloaded commons-lang3-3.12.0.pom via mrm.repository.url from CustomBasePathTest, so it should appear in the parent local repo +assert new File( basedir, '../../../../../local-repo/org/apache/commons/commons-lang3/3.12.0/commons-lang3-3.12.0.pom').exists() diff --git a/mrm-maven-plugin/src/it/custom-base-path/src/it/settings.xml b/mrm-maven-plugin/src/it/custom-base-path/src/it/settings.xml new file mode 100644 index 00000000..0c621bac --- /dev/null +++ b/mrm-maven-plugin/src/it/custom-base-path/src/it/settings.xml @@ -0,0 +1,62 @@ + + + + + + + + it-repo + @mrm.repository.url@ + * + + + + + it-repo + + + local.central + http://local.central + + true + + + true + + + + + + local.central + http://local.central + + true + + + true + + + + + + + it-repo + + diff --git a/mrm-maven-plugin/src/it/hostedrepo/pom.xml b/mrm-maven-plugin/src/it/hostedrepo/pom.xml index fa71f768..52b712bd 100644 --- a/mrm-maven-plugin/src/it/hostedrepo/pom.xml +++ b/mrm-maven-plugin/src/it/hostedrepo/pom.xml @@ -13,7 +13,7 @@ org.apache.maven.plugins maven-invoker-plugin - 3.2.2 + @maven-invoker-plugin.version@ diff --git a/mrm-maven-plugin/src/it/hostedrepo/src/it/local-maven-repo-proxy/invoker.properties b/mrm-maven-plugin/src/it/hostedrepo/src/it/local-maven-repo-proxy/invoker.properties new file mode 100644 index 00000000..ece61424 --- /dev/null +++ b/mrm-maven-plugin/src/it/hostedrepo/src/it/local-maven-repo-proxy/invoker.properties @@ -0,0 +1 @@ +invoker.goals = deploy diff --git a/mrm-maven-plugin/src/it/hostedrepo/src/it/local-maven-repo-proxy/pom.xml b/mrm-maven-plugin/src/it/hostedrepo/src/it/local-maven-repo-proxy/pom.xml new file mode 100644 index 00000000..1e4ab3ff --- /dev/null +++ b/mrm-maven-plugin/src/it/hostedrepo/src/it/local-maven-repo-proxy/pom.xml @@ -0,0 +1,18 @@ + + + + 4.0.0 + + + + mrm + @mrm.distribution.url@ + + + + org.mojohaus.mrm.hostedrepo.its + deploy + 1.0.0 + pom + + diff --git a/mrm-maven-plugin/src/it/hostedrepo/src/it/local-maven-repo-proxy/verify.groovy b/mrm-maven-plugin/src/it/hostedrepo/src/it/local-maven-repo-proxy/verify.groovy new file mode 100644 index 00000000..9035b087 --- /dev/null +++ b/mrm-maven-plugin/src/it/hostedrepo/src/it/local-maven-repo-proxy/verify.groovy @@ -0,0 +1,4 @@ +assert new File( basedir, '../../hosted-repo/org/mojohaus/mrm/hostedrepo/its/deploy/maven-metadata.xml').exists() +assert new File( basedir, '../../hosted-repo/org/mojohaus/mrm/hostedrepo/its/deploy/1.0.0/deploy-1.0.0.pom').exists() +assert new File( basedir, '../../hosted-repo/org/mojohaus/mrm/hostedrepo/its/deploy/1.0.0/deploy-1.0.0.pom.md5').exists() +assert new File( basedir, '../../hosted-repo/org/mojohaus/mrm/hostedrepo/its/deploy/1.0.0/deploy-1.0.0.pom.sha1').exists() diff --git a/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/AbstractStartMojo.java b/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/AbstractStartMojo.java index 6759fdd3..9cfd4b12 100644 --- a/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/AbstractStartMojo.java +++ b/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/AbstractStartMojo.java @@ -41,6 +41,18 @@ public abstract class AbstractStartMojo @Parameter( property = "mrm.port" ) private int port; + /** + * The base path under which the repository will be served. + *

+ * By default, {@code org.acme:my-artifact:pom:1.2.3} will be served under + * {@code http://localhost:/org/acme/my-artifact/1.2.3/my-artifact-1.2.3.pom}. + *

+ * If {@code basePath} is set to e.g. {@code foo/bar} then {@code org.acme:my-artifact:pom:1.2.3} will be served + * under {@code http://localhost:/foo/bar/org/acme/my-artifact/1.2.3/my-artifact-1.2.3.pom}. + */ + @Parameter( property = "mrm.basePath", defaultValue = "/" ) + private String basePath; + /** * The repositories to serve. When more than one repository is specified, a merged repository view * of those will be used. If none specified then a proxy of the invoking Maven's repositories will @@ -59,13 +71,14 @@ protected FileSystemServer createFileSystemServer( ArtifactStore artifactStore ) { return new FileSystemServer( ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() ), Math.max( 0, Math.min( port, 65535 ) ), - new AutoDigestFileSystem( new ArtifactStoreFileSystem( artifactStore ) ), + basePath, + new AutoDigestFileSystem( new ArtifactStoreFileSystem( artifactStore ) ), getSettingsServletPath() ); } /** - * When set, this points to the to the location from where the settings file can be downloaded. - * + * When set, this points to the to the location from where the settings file can be downloaded. + * * @return the servlet path to the settings file of {@code null} */ protected String getSettingsServletPath() diff --git a/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/FileSystemServer.java b/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/FileSystemServer.java index e9493ef8..8d99b5f7 100644 --- a/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/FileSystemServer.java +++ b/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/FileSystemServer.java @@ -95,7 +95,12 @@ public class FileSystemServer * The port to try and serve on. */ private final int requestedPort; - + + /** + * The context path where to bind the {@code fileSystem}. + */ + private final String contextPath; + /** * The path to settingsFile containing the configuration to connect to this repository manager. */ @@ -108,14 +113,37 @@ public class FileSystemServer * @param port The port to server on or 0 to pick a random, but available, port. * @param fileSystem the file system to serve. */ - public FileSystemServer( String name, int port, FileSystem fileSystem, String settingsServletPath ) + public FileSystemServer( String name, int port, String contextPath, FileSystem fileSystem, String settingsServletPath ) { this.name = name; this.fileSystem = fileSystem; this.requestedPort = port; + this.contextPath = sanitizeContextPath(contextPath); this.settingsServletPath = settingsServletPath; } + /** + * Sanitize the given {@code contextPath} by prepending slash if necessary and/or removing the trailing slash if necessary + * @param contextPath the contextPath to sanitize + * @return sanitized {@code contextPath} + */ + static String sanitizeContextPath(String contextPath) + { + if ( contextPath == null || contextPath.isEmpty() || contextPath.equals("/") ) + { + return "/"; + } + if ( !contextPath.startsWith("/") ) + { + return "/" + contextPath; + } + if ( contextPath.endsWith("/") ) + { + return contextPath.substring( 0, contextPath.length() - 1 ); + } + return contextPath; + } + /** * Ensures that the file system server is started (if already starting, will block until started, otherwise starts * the file system server and blocks until started) @@ -235,20 +263,21 @@ public int getPort() */ public String getUrl() { - return "http://localhost:" + getPort(); + return "http://localhost:" + getPort() + (contextPath.equals("/") ? "" : contextPath); } /** * Same as {@link #getUrl()}, but now for remote users - * - * @return the scheme + raw IP address + port - * @throws UnknownHostException if the local host name could not be resolved into an address. + * + * @return the scheme + raw IP address + port + contextPath + * @throws UnknownHostException if the local host name could not be resolved into an address. */ public String getRemoteUrl() throws UnknownHostException { - return "http://" + InetAddress.getLocalHost().getHostAddress() + ":" + getPort(); + return "http://" + InetAddress.getLocalHost().getHostAddress() + ":" + getPort() + + (contextPath.equals("/") ? "" : contextPath); } - + /** * The work to monitor and control the Jetty instance that hosts the file system. */ @@ -264,7 +293,7 @@ public void run() Server server = new Server(requestedPort); try { ServletContextHandler context = new ServletContextHandler(); - context.setContextPath("/"); + context.setContextPath(contextPath); context.addServlet(new ServletHolder(new FileSystemServlet(fileSystem, settingsServletPath)), "/*"); server.setHandler(context); server.start();