0

I am trying to utilize an existing Spring Boot application that is hosted on my company's Maven repository. This application takes messages from a Kafka server and puts them in a Mongo database. All that is needed to make it work is supply it with a properties file that defines the Kafka and Mongo servers. I need to create a Maven project that imports this application as a dependency, and supplies it with a properties file for the configuration. The only two files should be a pom.xml and application.properties. The end result should be a runnable JAR that executes the dependency (the existing Spring Boot application) using my properties file.

I do not have a choice in taking a different approach in solving this.

I have spent countless hours trying to configure Maven to do this. I had finally succeeded in getting this to work when using the mvn spring-boot:run command, but I cannot get it to produce a properly working runnable JAR file.

I include my dependency like any other:

<dependencies>
  <dependency>
    <groupId>com.company.dept</groupId>
    <artifactId>consume-to-mongo-app</artifactId>
    <version>1.0</version>
  </dependency>
</dependencies>

Then, I define the main class from the dependency like this:

<properties>
  <mainClass>org.springframework.boot.loader.JarLauncher</mainClass>
</properties>

And, my plugins section looks like this:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
          <mainClass>org.springframework.boot.loader.JarLauncher</mainClass>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>repackage</goal>
        </goals>
        <configuration>
          <classifier>exec</classifier>
              <mainClass>org.springframework.boot.loader.JarLauncher</mainClass>
        </configuration>
      </execution>
    </executions>
</plugin>

Application runs fine when running mvn spring-boot:run

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.4.RELEASE)

After running mvn clean package, then java -jar myjar.jar I get thousands of these:

)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Caused by: java.lang.reflect.InvocationTargetException
    ... 1024 more
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
zgillis
  • 175
  • 3
  • 14
  • Are you allowed to create a zip file that contains the properties and the jar? If that's unzipped and the properties file is in the same directory as the Spring Boot Jar, Spring Boot App will pick it up automatically. – Compass Apr 19 '19 at 18:15
  • I will suggest you to convert your project to a Maven multi module project. You will have two modules (projects) : your Spring project and the second one that contains your properties files. Then set a dependency between your modules. The jar generated should contains all your files (including the properties files). – Harry Coder Apr 19 '19 at 19:43
  • This link could help you : https://stackoverflow.com/questions/52673216/spring-boot-multi-module-unable-to-read-properties-file-from-another-module – Harry Coder Apr 19 '19 at 21:28
  • I don't think the issue is as much it not finding the properties file. My problem is once I assemble a JAR file, it throws a crazy amount of reflection exceptions (thousands). So the issue must be coming from something else spring-boot related. – zgillis Apr 22 '19 at 14:15
  • with Spring-boot 2 there seems to have changed https://www.baeldung.com/spring-boot-command-line-arguments – borjab May 18 '21 at 10:37

1 Answers1

0

I was able to solve my issue by using the Maven Assembly Plugin to create a custom-packed ZIP file containing my dependency JAR and my properties file. On the deployment server, this ZIP can be extracted, and the jar can be run in the resulting directory and it will pick up the properties file. I followed this tutorial: https://medium.com/@kasunpdh/using-the-maven-assembly-plugin-to-build-a-zip-distribution-5cbca2a3b052.

I made a few modifications from the tutorial to have my final ZIP structure directly contain my Spring Boot App dependency's JAR and my properties file with no other directories contained. This is my modified zip.xml file:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
  <!--<modelVersion>4.0.0</modelVersion>-->
  <id>zip</id>
  <includeBaseDirectory>true</includeBaseDirectory>

  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.basedir}/src/main/resources</directory>
      <outputDirectory>.</outputDirectory>
    </fileSet>
  </fileSets>
  <dependencySets>
    <dependencySet>
      <outputDirectory>.</outputDirectory>
      <excludes>
        <exclude>${project.groupId}:${project.artifactId}:jar:*</exclude>
      </excludes>
    </dependencySet>
  </dependencySets>

</assembly>
zgillis
  • 175
  • 3
  • 14