Publish a local markdown file to sites at once by Java, Configuration

Detail Explanation of project setup and configuration

·

5 min read

1. Related Post

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.

image.png

4.2. Folder Structure

image.png

Folder NameDescription
src/main/javajava code files under the package structure
src/main/testunit test java files under the package structure
src/main/resourcesfiles 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 put log4j2.xml in the classpath)

    image.png

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.

image.png

4.3.2. Specify main class

After I ran the maven package command, it prompted the following error.

image.png

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.

image.png

I need to do the following things to solve this issue:

  • Added lib into classpath of maven-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 insides plugins to tell maven to run copy dependencies when we run maven 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.

image.png

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>

image.png

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>

image.png

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>
    

    image.png

  • 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

    image.png

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 file log4j2.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.

    image.png

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)

    image.png

  • value: file extension of the language (used in creating file name in Gist)

    image.png

Content of the file

{
    "python": "py",
    "javascript": "js",
    "typescript": "ts",
    "cmd": "cmd",
    "html": "html",
    "json": "json"
}

Did you find this article valuable?

Support Ivan Yu by becoming a sponsor. Any amount is appreciated!