0

In order to avoid code duplication, I decided to pack some classes in the spring based library project. Below are the steps I took:

  1. Created project using spring initialzr.
  2. Created desired structure and add classes.
  3. Deleted main class
  4. Added to pom.xml required dependencies
  5. Added to pom.xml plugin maven-jar-plugin

enter image description here

  1. Created simple script to automate packaging and installing dependency to my local maven repository:
    #!/usr/bin/env bash

    POM="pom.xml"
    ARTIFACT="springbot-messenger-client"

    MVN_VERSION=$(mvn -q -f $POM \
        -Dexec.executable=echo \
        -Dexec.args='${project.version}' \
        --non-recursive \
        exec:exec)

    mvn package -f $POM
    mvn install:install-file \
    -Dfile="target/"$ARTIFACT"-"$MVN_VERSION".jar" \
    -DgroupId=ai.optime \
    -DartifactId=$ARTIFACT \
    -Dversion=$MVN_VERSION \
    -Dpackaging=jar

    echo "Version "$MVN_VERSION" of "$ARTIFACT" installed"

  1. Script execution succeeded and I've added my newly created dependency to another project.

During application startup, I've got an exception:

Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.dataformat.yaml.YAMLFactory

Class YAMLFactory belongs to dependency jackson-dataformat-yaml which is added to pom.xml of library project:

enter image description here

From my understanding, mvn package does not include dependencies, so how to include them properly ? I know the concept of fat jar but this is probably not the right solution.

kamil
  • 39
  • 1
  • 6
  • The build and installation can simply being done via: `mvn clean package` will create everything ...and a `mvn clean install` will install the artifact into your local repository which usually is not what you like ....in Spring Boot you usually do `mvn spring-boot:run` to start the app on command line or via IDE .... – khmarbaise Feb 05 '20 at 13:41

1 Answers1

0

What I use to compile a spring application is:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler.version}</version>
            <configuration>
                <source>11</source>
                <target>11</target>
                <release>11</release>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <mainClass>path.to.main.class</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

And with

mvn clean install

, it will generate a jar with all dependencies.

Hope it helps you.

guren
  • 122
  • 2
  • 9