Close

Upload Maven Artifacts On Remote Server via FTP without any Repository Manager

[Last Updated: Jan 21, 2019]

This tutorial shows how to host maven projects on a remote server without using a full-fledged repository manager. This can be useful if you quickly want to share your artifacts among different computers or developers. You need to have a web hosting plan which supports FTP as well.

We are going to use Maven Wagon Plugin which is used to transport Maven's artifacts. It supports FTP as well.

Using example hosting site

I am going to create an example hosting account on 000webhost (free shared hosting). After going through registration process I have following FTP information:

Let's have FTP access via FileZilla. Create a new connection with above FTP information and create a directory acting as maven repository:

We created our maven-repository under public_html so that it can be accessed via HTTP by the clients who want to import dependencies from here.

Creating Example Project

Let's create a maven project.

pom.xml

<?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>com.logicbig.example</groupId>
    <artifactId>maven-example-project</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
        <extensions>
            <extension>
                <groupId>org.apache.maven.wagon</groupId>
                <artifactId>wagon-ftp</artifactId>
                <version>3.2.0</version>
            </extension>
        </extensions>
    </build>
    <distributionManagement>
        <repository>
            <id>my-example-site-ftp</id>
            <url>ftp://files.000webhost.com/public_html/maven-repository</url>
        </repository>
    </distributionManagement>
</project>

As seen above we added the extension wagon-ftp which is used for transporting the artifacts. A maven extension is a way to add libraries to maven core classloader (this is different than plugin classloaders).

In above pom we also specified our remote maven repository location under repository of distributionManagement.

Distribution management manages the distribution of the artifacts and other supporting files generated throughout the build process.

We should specify the FTP username/password separately in settings.xml under <user_home>/.m2/settings.xml:

<settings 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/settings-1.0.0.xsd">
  <servers>
   <server>
      <id>my-example-site-ftp</id>
      <username>example-site2</username>
      <password>my-test-password</password>
    </server>
  </servers>
</settings>

Above settings and the repository specified in our pom.xml are linked via 'id' (i.e. my-example-site-ftp).

Let's also add a Java class to our example project so that we can access that on client site:

public class TestClass {
  public static void test() {
      System.out.println("In Test Class");
  }
}

Deploying our project

To upload our project to the server we need to use mvn deploy command:

D:\maven-ftp-repo-example\maven-example-project>>mvn deploy
[INFO] Scanning for projects...
[INFO]
[INFO] -------------< com.logicbig.example:maven-example-project >-------------
[INFO] Building maven-example-project 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-example-project ---
[INFO] skip non existing resourceDirectory D:\example-projects\maven\maven-ftp-repo-example\maven-example-project\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-example-project ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-example-project ---
[INFO] skip non existing resourceDirectory D:\example-projects\maven\maven-ftp-repo-example\maven-example-project\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-example-project ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-example-project ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-example-project ---
[INFO] Building jar: D:\example-projects\maven\maven-ftp-repo-example\maven-example-project\target\maven-example-project-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-example-project ---
[INFO] Installing D:\example-projects\maven\maven-ftp-repo-example\maven-example-project\target\maven-example-project-1.0-SNAPSHOT.jar to C:\Users\Joe\.m2\repository\com\logicbig\example\maven-example-project\1.0-SNAPSHOT\maven-example-project-1.0-SNAPSHOT.jar
[INFO] Installing D:\example-projects\maven\maven-ftp-repo-example\maven-example-project\pom.xml to C:\Users\Joe\.m2\repository\com\logicbig\example\maven-example-project\1.0-SNAPSHOT\maven-example-project-1.0-SNAPSHOT.pom
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ maven-example-project ---
Downloading from my-example-site-ftp: ftp://files.000webhost.com/public_html/maven-repository/com/logicbig/example/maven-example-project/1.0-SNAPSHOT/maven-metadata.xml
Uploading to my-example-site-ftp: ftp://files.000webhost.com/public_html/maven-repository/com/logicbig/example/maven-example-project/1.0-SNAPSHOT/maven-example-project-1.0-20190120.060800-1.jar
Progress (1): 2.6 kB

Uploaded to my-example-site-ftp: ftp://files.000webhost.com/public_html/maven-repository/com/logicbig/example/maven-example-project/1.0-SNAPSHOT/maven-example-project-1.0-20190120.060800-1.jar (2.6 kB at 979 B/s)
Uploading to my-example-site-ftp: ftp://files.000webhost.com/public_html/maven-repository/com/logicbig/example/maven-example-project/1.0-SNAPSHOT/maven-example-project-1.0-20190120.060800-1.pom
Progress (1): 1.3 kB

Uploaded to my-example-site-ftp: ftp://files.000webhost.com/public_html/maven-repository/com/logicbig/example/maven-example-project/1.0-SNAPSHOT/maven-example-project-1.0-20190120.060800-1.pom (1.3 kB at 632 B/s)
Downloading from my-example-site-ftp: ftp://files.000webhost.com/public_html/maven-repository/com/logicbig/example/maven-example-project/maven-metadata.xml
Uploading to my-example-site-ftp: ftp://files.000webhost.com/public_html/maven-repository/com/logicbig/example/maven-example-project/1.0-SNAPSHOT/maven-metadata.xml
Progress (1): 785 B

Uploaded to my-example-site-ftp: ftp://files.000webhost.com/public_html/maven-repository/com/logicbig/example/maven-example-project/1.0-SNAPSHOT/maven-metadata.xml (785 B at 394 B/s)
Uploading to my-example-site-ftp: ftp://files.000webhost.com/public_html/maven-repository/com/logicbig/example/maven-example-project/maven-metadata.xml
Progress (1): 299 B

Uploaded to my-example-site-ftp: ftp://files.000webhost.com/public_html/maven-repository/com/logicbig/example/maven-example-project/maven-metadata.xml (299 B at 167 B/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.317 s
[INFO] Finished at: 2019-01-20T00:08:09-06:00
[INFO] ------------------------------------------------------------------------

In FileZilla, check our new deployed artifacts (by refreshing the explorer):

We have successfully uploaded our artifacts via FTP.

Client Project

Let's see how to use our remote repository to resolve dependencies in an example client project.

If you are using the same computer then you have to delete the local .m2 cache of our above project (created by mvn deploy) otherwise maven will resolve dependency locally instead of fetching that from the remote server.

Let's create our client project.

maven-example-client/pom.xml

<?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>com.logicbig.example</groupId>
    <artifactId>maven-example-client</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <repositories>
        <repository>
            <id>example-repo</id>
            <url>https://example-site2.000webhostapp.com/maven-repository</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.logicbig.example</groupId>
            <artifactId>maven-example-project</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
    </plugins>
    </build>

</project>

An example client Java class:

public class TestClient{
  public static void main(String[] args) {
      TestClass.test();
  }
}

Let's execute above class:

D:\maven-ftp-repo-example\maven-example-client>mvn  exec:java -Dexec.mainClass="com.logicbig.example.TestClient"
[INFO] Scanning for projects...
[INFO]
[INFO] -------------< com.logicbig.example:maven-example-client >--------------
[INFO] Building maven-example-client 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from example-repo: https://example-site2.000webhostapp.com/maven-repository/com/logicbig/example/maven-example-project/1.0-SNAPSHOT/maven-metadata.xml
Downloaded from example-repo: https://example-site2.000webhostapp.com/maven-repository/com/logicbig/example/maven-example-project/1.0-SNAPSHOT/maven-metadata.xml (785 B at 1.5 kB/s)
Downloading from example-repo: https://example-site2.000webhostapp.com/maven-repository/com/logicbig/example/maven-example-project/1.0-SNAPSHOT/maven-example-project-1.0-20190120.060800-1.pom
Downloaded from example-repo: https://example-site2.000webhostapp.com/maven-repository/com/logicbig/example/maven-example-project/1.0-SNAPSHOT/maven-example-project-1.0-20190120.060800-1.pom (1.3 kB at 7.2 kB/s)
Downloading from example-repo: https://example-site2.000webhostapp.com/maven-repository/com/logicbig/example/maven-example-project/1.0-SNAPSHOT/maven-example-project-1.0-20190120.060800-1.jar
Downloaded from example-repo: https://example-site2.000webhostapp.com/maven-repository/com/logicbig/example/maven-example-project/1.0-SNAPSHOT/maven-example-project-1.0-20190120.060800-1.jar (2.6 kB at 19 kB/s)
[INFO]
[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ maven-example-client ---
In Test Class
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.020 s
[INFO] Finished at: 2019-01-20T01:21:19-06:00
[INFO] ------------------------------------------------------------------------

The dependencies are downloaded only at the first access and kept in .m2 cache.

Making repository private

We can also make our repository private by making our maven-repository folder .htaccess password protected (as described here). This applies to the HTTP access by the client. In that case we need to specify the related username/password for the client project (in repository section or by adding that in settings.xml).



Note that above hosting account was created only for demo purpose and has been deleted afterwards.

Example Project

Dependencies and Technologies Used:

  • JDK 11
  • Maven 3.5.4

Hosting maven artifacts on a FTP server Select All Download
  • maven-ftp-repo-example
    • maven-example-client
      • pom.xml
      • maven-example-project

    See Also