“Blog move” original address: Jane book original publication time: 2017-04-06

1. Set the Java JDK version to JDK 1.8.

You can modify pom.xml to use Java 8 language features in either of two ways by adding the following statement:

1.1 add the property

<project>[...].<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>[...].</project>
Copy the code

1.2 Configuring plug-ins directly

<project>[...].<build>[...].<plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1 track</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>[...].</build>[...].</project>
Copy the code

2. Maven builds generate executable jars

2.1 Simply build an executable Jar

<project>[...].<build>[...].<plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>[...].<archive>
            <manifest>
              <mainClass>org.sample.App</mainClass>
            </manifest>
          </archive>
        </configuration>[...].</plugin>[...].</project>
Copy the code

To build the executable Jar, run the following command

mvn assembly:single
Copy the code

2.2 Bind Assembly’s single goal to the build life cycle of your project

You can add the following to the pom.xml file:

<project>[...].<build>[...].<plugins>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>3.0.0</version>
          <configuration>
            <! -- append assembly id in release file name -->
            <appendAssemblyId>true</appendAssemblyId>
            <! Build an executable Jar-->
            <archive>
              <manifest>
                <mainClass>cc.bitky.fx.Main</mainClass>
              </manifest>
            </archive>
            <! -- Use the "Jar integration dependency" descriptor -->
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
          </configuration>
          <executions>
            <execution>
              <id>make-assembly</id>
              <! -- this is used for inheritance merges -->
              <phase>package</phase>
              <! -- bind to the packaging phase -->
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>[...].</build>
</project>
Copy the code

Then, to generate the Jar files for your project, you can simply execute the following lifecycle phase command:

mvn package
Copy the code

After the build is complete, you can see that you have generated files with names like the following:

Target/sample-1.0-snapshot-jar-with-dependencies. Jar target/sample-1.0.jarCopy the code

AppendAssemblyId: Controls whether to include the “ssembly ID” in the file name of the generated file.

Execution: Used to integrate the Maven-assembly-plugin into the standard Maven build life cycle, at which point the specified operations are performed when the Maven package is executed to achieve custom packaging.

2.3 Maven Goals for the Assembly plug-in

  • Assembly: Assembly: “deprecated” automatically executes the package lifecycle.
  • Assembly: Single: only assembs into jar-with-dependencies. We don’t want the Package phase to run package twice, so we configure this.

3. Garbled characters occur when the executable Jar file is executed

Using the executable Jar file generated by Maven, garbled characters appear when executing in CMD. The project code is utF-8. SLF4J framework is used for logs.

3.1 Method 1: Windows uses GBK encoding by default. The encoding is dynamically specified during Jar execution

Add parameters when running the java-jar command

-Dfile.encoding=UTF-8 
Copy the code

The format is as follows:

java -jar -Dfile.encoding=UTF-8 simpler.jar 
Copy the code

Or add environment variables:

JAVA_TOOL_OPTIONS = -Dfile.encoding=UTF-8
Copy the code

3.2 Run with a temporary active code page

Execute the command from the console:

chcp 65001
Copy the code

You can change the current CMD code temporarily to UTF-8. Run the CHCP command to display the current CMD code.

4. Read resource files from Maven

In the Maven project root directory, there is the following necessary directory structure:

  • src
    • main
      • Save some resources files.
      • java
    • test
      • Save some resources files.
      • java
  • pom.xml

Target /classes/ directory, all resource files and.class files are copied to target/classes/ directory during compile time.

1. thisGetClass (). GetResource ("")// Get the URL of the directory where the current class file is located.

2. thisGetClass (). GetResource ("/")3. thisGetClass (). GetClassLoader (). GetResource ("")4. This. GetSystemResource ("")5. Thread. CurrentThread (). GetContextClassLoader (). The getResource ("")// Get the absolute URI path of the current ClassPath.
Copy the code

So, if you want to get the resource file ky.xml from the Resources folder, you can use one of the following statements:

getClass().getClassLoader().getResource("ky.xml")
getClass().getResource("/ky.xml")
Copy the code

5. Reference materials

  1. Setting the -source and -target of the Java Compiler
  2. 5 things you didn’t know about Apache Maven
  3. Maven packages executable Jar methods
  4. [about creating an executable Jar file] (http://tonglin.iteye.com/blog/556449)
  5. Beginner Maven – Implement custom packaging using the Assembly Plugin
  6. JavaFX and maven: NullPointerException: Location is required
  7. Java gets the file path
  8. Maven projects perform log garble when packaged as JARS
  9. The Console output utf-8