Search code examples
javadockermavenjava-17

How to get Maven to resolve dependencies from local repository


I am trying to build a Java project with Maven, but it is not able to find an artifact from a local jar file. I am attempting to follow instructions from here. The library I am trying to use was packaged by Maven and has groupId gov.nasa.drf and artifactId lib.

My build instructions:

mvn clean
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=./repo/gov/nasa/drf/lib/lib-1.0.jar
mvn compile
mvn package

The install runs successfully, but the compile step fails with

Failed to execute goal on project auth: Could not resolve dependencies for project gov.nasa.drf:auth:jar:2.0: Could not find artifact gov.nasa.drf:lib:jar:1.0 in data-local (file:///buildDir/repo)

For deployment (and consistency) I am running the build commands in Docker. My dockerfile:

FROM maven:3-openjdk-17 AS build

COPY ./src /buildDir/src
COPY ./repo /buildDir/repo
COPY pom.xml /buildDir/pom.xml
RUN mkdir ~/.m2

WORKDIR /buildDir
RUN mvn clean 
RUN mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=./repo/gov/nasa/drf/lib/lib-1.0.jar
RUN mvn compile 
RUN mvn package 

FROM openjdk:17-oracle
RUN mkdir -p /api/target
RUN mkdir -p /api/src/main
RUN mkdir /api/keys
RUN mkdir /api/logs
COPY src/main/resources /api/src/main/resources
COPY --from=build /buildDir/target /api/target
COPY pom.xml /api/pom.xml
WORKDIR /api
EXPOSE 8082
CMD ["java", "-jar", "/api/target/auth-2.0.jar"]

My 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>gov.nasa.drf</groupId>
    <artifactId>auth</artifactId>
    <version>2.0</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <spring.version>6.1.0.RELEASE</spring.version>
        <aspectj.version>1.9.19</aspectj.version>
        <log4j2.version>2.17.1</log4j2.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.1</version>
    </parent>

    <repositories>
        <repository>
            <id>data-local</id>
            <name>data</name>
            <url>file://${project.basedir}/repo</url>
        </repository>
    </repositories>

    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>gov.nasa.drf.auth.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.1.2</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>3.1.2</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <parallel>both</parallel>
                    <threadCount>4</threadCount>
                    <includes>
                        <include>**/*Test</include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.14.0</version>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <outxml>true</outxml>
                    <verbose>true</verbose>
                    <showWeaveInfo>true</showWeaveInfo>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-aspects</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>

        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>gov.nasa.drf</groupId>
            <artifactId>lib</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.joschi.jackson</groupId>
            <artifactId>jackson-datatype-threetenbp</artifactId>
            <version>2.13.0-rc2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.28</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
    </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
    </dependencies>
</project>

Solution

  • With install-file, you need to add the Maven coordinates (GroupId, ArtifactId, Version) as parameters so that the JAR is placed at the correct place.