Photo by Ferenc Almasi on Unsplash
Publish a local markdown file to sites at once by Java, Configuration
Detail Explanation of project setup and configuration
1. Related Post
Publish a local markdown file to sites at once by Java, Usage
Publish a local markdown file to sites at once by Java, Problem-Solving & Enhancement
2. Introduction
After introducing the usage of the program, we will explain the program structure and configuration.
3. Project Setup
Please follow the README.md to set up the project.
4. Project Structure
4.1. Project Template
I used the project template maven-archetype-quickstart
to develop this program.
4.2. Folder Structure
Folder Name | Description |
src/main/java | java code files under the package structure |
src/main/test | unit test java files under the package structure |
src/main/resources | files you need to include in the jar file |
4.2.1. Remark
src/main/resources
will NOT be generated automatically so you need to create it manually.Add
src/main/resources
to the build path if necessary, (e.g., in the project, we need to add it in order to putlog4j2.xml
in the classpath)
4.3. Maven
In this section, I would like to explain some specific settings I made in pom.xml
4.3.1. Specify Java version
After creating a new maven project, maven.compiler.source
and maven.compiler.target
point to the 1.7 or an older Java version. We need to change to the new version(e.g. 17 in our case).
<properties>
...
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
Also, remember to change it in project properties in Eclipse as well.
4.3.2. Specify main class
After I ran the maven package
command, it prompted the following error.
To solve this issue, we need to specify the main class of the jar by adding <mainClass>
com.blog.publish.publisher.App
</mainClass>
to tell the jar which class main method we would like to execute.
<plugin>
<artifactId>maven-jar-plugin</artifactId>
...
<configuration>
<archive>
<manifest>
...
<mainClass>com.blog.publish.publisher.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
4.3.3. Copy dependencies to target/lib
folder during maven package
During development, after I built the jar file and run the jar file by command, it turned out that it cannot find the dependencies I imported to the project and saw the error below.
I need to do the following things to solve this issue:
Added
lib
intoclasspath
ofmaven-jar-plugin
to tell the jar where to find the dependencies<plugin> <artifactId>maven-jar-plugin</artifactId> ... <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> ... </manifest> </archive> </configuration> </plugin>
Added
maven-dependency-plugin
and specify the output directory${
project.build.directory
}/lib
to tell maven where we want to copy the dependencies to<plugin> <artifactId>maven-dependency-plugin</artifactId> <version>3.3.0</version> <executions> <execution> ... <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> ... </configuration> </execution> </executions> </plugin>
Put
maven-dependency-plugin
insidesplugins
to tell maven to run copy dependencies when we runmaven package
<plugin> <artifactId>maven-dependency-plugin</artifactId> ... <executions> <execution> ... <phase>package</phase> ... </execution> </executions> </plugin>
<plugins> <plugin> <artifactId>maven-dependency-plugin</artifactId> </plugin> ... </plugins>
When you run maven package
, you can see something below.
4.3.4. Copy files to the target folder during maven package
We would like to copy 2 configuration files tokens.properties
and language2Ext.json
to the target folder whenever we run maven package
.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
...
<executions>
<execution>
<id>copy-resource-tokens</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<directory>${basedir}</directory>
<includes>
<include>tokens.properties</include>
<include>language2Ext.json</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
4.3.5. Create temp
folder during maven package
We would like to create a temp
directory for us to store the gist ids. (I will explain in the next blog why we need this.)
We will achieve this by adding maven-antrun-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>tempFolder</id>
<phase>package</phase>
<configuration>
<target>
<echo message="Creating temporary directory" />
<delete dir="${project.build.directory}/temp" />
<mkdir dir="${project.build.directory}/temp" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Same as copy dependencies, we need to put maven-dependency-plugin insides plugins to tell maven to run creating temp
directory during maven package
.
<plugins>
...
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
</plugin>
</plugins>
5. log4j2
Every program needs logging and one of the most popular libraries to handle logging in Java is log4j
.
5.1. Maven, pom.xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.18.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.18.0</version>
</dependency>
5.2. Configuration
All the log setting is placed in src/main/resources/log4j2.xml
5.2.1. Setting
I would like to show you the configuration meaning:
Date, time and level (INFO, ERROR) are included in log message.
<Property name="LOG_PATTERN">[%p] [%d] [%c{1}], %m%n</Property>
log message is printed in the console and at the same time, stored in the file
<AppenderRef ref="console" /> <AppenderRef ref="flog" />
log file is stored according to date, link
5.3. Remark
There is a well-known Log4j Security vulnerabilities so using version \>= 2.17.1 is recommended
There are a lot of ways to configure
log4j
. If you have chosen to use the xml filelog4j2.xml
configuration method like me, please put the file in the classpath, as mentioned in the link.XML ConfigurationFactory will look for log4j2.xml in the classpath.
Also for standard practices, please put it insides
src/main/resources
so when we package the program to a jar file,log4j2.xml
will be included.
6. language2Ext.json
This file stores a programming language to file extension mapping, which is for uploading code block to Git Gist.
Mapping structure:
key: programming language (specifies in the code block)
value: file extension of the language (used in creating file name in Gist)
Content of the file
{
"python": "py",
"javascript": "js",
"typescript": "ts",
"cmd": "cmd",
"html": "html",
"json": "json"
}