简体   繁体   中英

How do I use Maven to Utilize Properties File with Existing Spring Boot Application?

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM