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.
First, build a project to use for testing.
Create the Maven project as usual. If you don't know how to create a Maven project, please see here.
For this project, we need dependencies and resources to copy, so we'll add them.
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->
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
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;
/**
Hello World with Maven */ public class HelloWorld { / ** Logger * / private static Logger logger = LoggerFactory.getLogger(HelloWorld.class);
/**
main processing
Display Hello World on standard output.
@param args command arguments */ public static void main(String[] args) { // Change standard output to logger logger.info("Hello Maven World!"); } }
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>
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.
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->
<!-Plugin for copying dependent jars->
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.
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.
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.
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