How to make a jar file with no dependencies in Maven

Introduction

This is the third in the Maven series. Earlier I wrote How to build an executable jar with Maven. Using this method, you can execute the java program archived in the jar file with the command java -jar <name of the jar file to be executed>.

However, the executable jar file built by this method will include all the dependencies used in the program in the jar file, so if you try to rewrite the contents of the configuration file etc., after modifying the source file You need to rebuild it.

Then, for example, a property file that puts out setting values that you do not want to write in the program, etc., will lose about half of the meaning that you put out.

If possible, do not bundle the configuration file etc. with the jar file, and put the configuration file Direct fix> Restart the program I want to finish it.

So this time, I will introduce how to build a jar file that does not include dependent jars and configuration files with Maven.

In the build method introduced this time, the entry point of the jar file is not specified, so the java program cannot be executed with the above command.

I don't know how to do it, but if anyone who has read this article knows how to do it, I would appreciate it if you could teach me how to do it.

It's been a long time, but it was a synopsis of how I came to write this article and what I'm about to write.

From here, I will describe the specific method.

1. Build a Maven project

First, build a project to use for testing.

1.1. Creating a project

Create the Maven project as usual. If you don't know how to create a Maven project, please see here.

1.2. Additional settings for testing

For this project, we need dependencies and resources to copy, so we'll add them.

1.2.1. Adding dependencies

Add the dependent jar file to see the copy of the dependency. The jar files that the Maven project depends on are listed in pom.xml. By doing so, Maven will automatically download the dependent jar files from the central repository.

■ Contents of pom.xml

<dependencies>
	<dependency>

<!-This time we will add the logger as a dependency-> ch.qos.logback logback-classic 1.2.3

1.3. Adding resources

Add a resource file to verify the copy of the resource. This time, add the logger (logback) configuration file (logback.xml) added so far as a resource file. Add the configuration file to the "src / main / resources" folder.

■ Project tree after adding resources picuture1.JPG

1.4. Modified to use logger

In the hello-world project, Hello World was output using standard output, but let's modify that part to a setting that uses a logger.

package hello.main;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**

1.5. Logback.xml settings

The configuration file that controls the movement of the logger is "logback.xml". logback itself works with the default settings without it, but this time there is also a copy test, so let's write the config file exactly. As a reminder of how to write logback.xml, which I always forget, the settings to output to the console and the settings to output to a file are described.

■ Contents of logback.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
		</encoder>
	</appender>

	<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>logs/log%d{yyyy-MM-dd}.log</fileNamePattern>
			<maxHistory>3</maxHistory>
		</rollingPolicy>
		<encoder>
			<pattern>%d{yyyy-MM-dd'T'HH:mm:ss'Z'} - %m%n</pattern>
		</encoder>
	</appender>

	<root level="DEBUG">
		<appender-ref ref="STDOUT" />
		<appender-ref ref="FILE" />
	</root>
</configuration>

2. Copy the dependent jar

First, set up a copy of the dependent jar files, including the libraries used in your program.

2.1. maven-dependency-plugin To copy the dependent files to a different directory than the jar files, use a plugin called maven-dependency-plugin. This plugin allows you to copy dependent jar files to a specified directory at build time.

2.2. Editing pom.xml

For such settings, add the settings for building the jar file to pom.xml as in the example. The point here is to set the copy excluding the materials used in the test, which is inherent in Maven. To be precise, you can't just set the dependency plugin, just limit the scope of dependent files to testing.

■ Contents of pom.xml

<!-Limit scope to testing-> junit junit 4.11 <!-Specify the scope here-> test

<!-Plugin for copying dependent jars-> org.apache.maven.plugins maven-dependency-plugin <!-Click here for the latest information at the time of writing-> 3.0.1 copy-dependencies package copy-dependencies <!-Copy only materials used in compile scope-> compile

3. Copy of resources

There are many meanings to a resource, but a resource here refers to a file that describes settings used in a java program.

Specifically, it is a file placed in the "src / main / resources /" directory of the Maven project. In the environment I often use, I often put the configuration file of the logging library, etc., as I do in project construction.

As an aside, I prefer to use the "logback + slf4j" environment.

3.1. maven-resources-plugin To copy resources such as configuration files, use a plugin called maven-resources-plugin.

3.2. Editing pom.xml

Now, let's add the plugin settings to pom.xml as well. The point here is to add the resource-free settings to the jar file at build time. If you do not make this setting, you will end up building with resources in the jar file.

■ Contents of pom.xml

<build>

<!-Here, the setting to exclude resources at build time is described. src/main/resources <!-Specify resources not included in the jar file with the excludes tag-> * org.apache.maven.plugins maven-resources-plugin 3.0.2 copy-resources package copy-resources ${basedir}/target/resources <!-As a bonus, also specify the directory to copy the resource to-> src/main/resources true

Finally

So far, we have introduced how to build files excluding jar-dependent files. I surfed the internet for quite some time until I got to this point, but I had a hard time because there was no Japanese web that I could use as it was. One of the reasons is that I don't have a deep understanding of Maven in the first place, but I hope that there are people in the same situation who can help them.

Also, if anyone knows how to do it smarter, I would appreciate it if you could point out and teach me that.

I've learned a lot about Maven these days, so I think this is the last Maven series for a while. I've written three articles so far, but Maven is a build tool, but it's a build tool. There are many ways to use it, and it is deep, so I may introduce it when I study differently somewhere.

EX. Maven series posted by the author

For reference, I will summarize the articles I have written so far as links. If you have read this article, please use the following as a reference.

■ [For super beginners] Maven super introduction http://qiita.com/ryota0001/items/e019ec4daaaf54684c53

■ How to build an executable jar with Maven http://qiita.com/ryota0001/items/e019ec4daaaf54684c53

Recommended Posts

How to make a jar file with no dependencies in Maven
How to make a Maven project
How to add jar file in ScalaIDE
Try to make a cross-platform application with JRuby (jar file generation)
How to read a library from a JAR file with VS Code << How to not use Maven / Gradle >>
How to make a follow function in Rails
How to build an executable jar in Maven
How to start a Docker container with a volume mounted in a batch file
How to make a factory with a model with polymorphic association
[How to insert a video in haml with Rails]
How to convert a file to a byte array in Java
How to debug the generated jar file in Eclipse
How to save a file with the specified extension under the directory specified in Java to the list
How to request a CSV file as JSON with jMeter
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Mapping to a class with a value object in How to MyBatis
How to set up a proxy with authentication in Feign
What to do if you are told "there is no main manifest attribute" when creating a jar file containing dependencies in a maven project
How to make a Java container
How to make a JDBC driver
How to make a splash screen
How to make a Jenkins plugin
[Beginner] How to delete NO FILE
How to make a Java array
How to create a jar file or war file using the jar command
How to create a new Gradle + Java + Jar project in Intellij 2016.03
How to run a job with docker login in AWS batch
Add a time stamp to the JAR file name in Gradle
How to rename a model with foreign key constraints in Rails
How to open a script file from Ubuntu with VS code
How to make a jar with old Hadoop (hadoop-core-0.20.2-cdh3u6) in Gradle: (What to do if you get Could not expand ZIP ..)
[Xcode] How to add a README.md file
How to add local jar to maven pom.xml
How to make a Java calendar Summary
Create a jar file with the command
How to insert a video in Rails
How to delete the migration file NO FILE
How to create a Maven repository for 2020
How to make a Discord bot (Java)
How to achieve file download with Feign
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to publish a library in jCenter
How to achieve file upload with Feign
[Personal memo] How to interact with a random number generator in Java
[Rails] How to log in with a name by adding a devise name column
How to create a server executable JAR and WAR with Spring gradle
If you want to make a zip file with Ruby, it's rubyzip.
How to read log4j configuration file in Java project summarized in jar file Memo
How to make an app with a plugin mechanism [C # and Java]
How to download and run a Jar package directly from the Maven repository with just the command line
Read a string in a PDF file with Java
How to display a web page in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
How to run a djUnit task in Ant
How to add a classpath in Spring Boot
How to create a theme in Liferay 7 / DXP
How to make a lightweight JRE for distribution
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to bind to property file in Spring Boot
Add jar file obtained from Maven to IntelliJ