0

how to create a local dependency with spring boot. I'm trying to create an application with a local dependency. I create the local dependency then import it into another spring boot project but it fails to import the local dependency classes into the other project.

This is the pom.xml of my local dependency:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>generateproto</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>generate-proto</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java-util</artifactId>
            <version>3.23.2</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
            <version>1.16.1</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty</artifactId>
            <version>1.16.1</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
            <version>1.55.1</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
            <version>1.16.1</version>
        </dependency>
        <dependency> <!-- necessary for Java 9+ -->
            <groupId>org.apache.tomcat</groupId>
            <artifactId>annotations-api</artifactId>
            <version>6.0.53</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>repositorio.local</id>
            <name>Meu repositorio Local</name>
            <url>file:../maven-repositorio</url>
        </repository>
    </repositories>

    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.1</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <configuration>
                    <protocArtifact>
                        com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier}
                    </protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>
                        io.grpc:protoc-gen-grpc-java:1.4.0:exe:${os.detected.classifier}
                    </pluginArtifact>
                    <clearOutputDirectory>false</clearOutputDirectory>
                    <outputDirectory>src/main/java</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

The command to create my local directory:

mvn install:install-file -Dfile=target/generateproto-0.0.1-SNAPSHOT.jar -DgroupId=com.example -DartifactId=generateproto -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -DlocalRepositoryPath=../maven-repositorio

My other pom.xml project:

<dependency>
    <groupId>com.example</groupId>
    <artifactId>generateproto</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

The folder structure looks like this:

  • -- another project ---local dependency ---maven-repositorio

But this framework fails to import the classes when I run the command:

mvn dependency:purge-local-repository

gives me the following error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:3.5.0:purge-local-repository (default-cli) on project cart-purchase-command: Failed to refresh project dependencies for: com.example:cart-purchase-command:jar:0.0.1-SNAPSHOT: required artifacts missing: [ERROR] com.example:generateproto:jar:0.0.1-SNAPSHOT

Does anyone know where I'm going wrong?

Thank you!

Rafael Souza
  • 1,143
  • 2
  • 13
  • 29
  • Life might be simpler if you just used the default local repository (~/.m2/repository) and `mvn install` instead of using a custom configuration. – tgdavies Aug 02 '23 at 02:39
  • I put this on kubernetes in docker images so I opted for this configuration. Thanks for the feedback – Rafael Souza Aug 02 '23 at 02:45
  • What may be happening is that `purge-local-repository` is trying to re-resolve the dependency, and failing, which is expected as it was manually installed. You can try setting that parameter to false https://maven.apache.org/plugins/maven-dependency-plugin/purge-local-repository-mojo.html#reResolve (also see https://stackoverflow.com/questions/40381906/what-does-purge-local-repository-actually-purge) – tgdavies Aug 02 '23 at 02:48
  • Yes thanks. but why can't I import the classes from the local repository? – Rafael Souza Aug 02 '23 at 02:52
  • What I think happens is that `purge-local-repository` deletes the dependencies of `cart-purchase-command` from `../maven-repositorio`, then tries to get `generateproto` from a remote repository, but can't because it didn't exist anywhere other than `../maven-repositorio`. – tgdavies Aug 02 '23 at 03:01
  • Why are you running `purge-local-repository`? – tgdavies Aug 02 '23 at 03:06
  • 1
    Remove the `spring-boot-maven-plugin` from the "local" project. It is just a jar that you want to use as a dependency, not a spring boot application. – M. Deinum Aug 02 '23 at 07:35
  • Thanks a lot it works @M. Deinum – Rafael Souza Aug 02 '23 at 19:52

0 Answers0