Use of Maven for building tools

1, Foreword

For the development of a java project, we will compile, test, package and deploy these construction processes before going online. If there are few files, we can use the commands Java – > javac – > jar to complete the above construction process. But as the project gets bigger and bigger, there are more and more files. Java's native commands seem to be stretched and not intentional. Maven manages jar package dependency based on POM and implements the project construction process through its own life cycle. The specific construction principle can be Baidu / Google. This article mainly shares some practical Maven tips for you.

2, Configuration skills

1. Development and configuration skills of multi office environment

When working in the company, relying on package loading will use the company's private server; When working from home, relying on package loading will use domestic images such as Alibaba / Netease. When switching the office environment, we want to switch in a simple way instead of changing maven's settings configuration file every time. What should we do? Refer to the following scheme:

<settings>
  	<profile>
        <id>company</id>
        <repositories>
            <repository>
                <id>nexus</id>
                <name>nexus-repositories</name>
                <url>http://xxxx.xxxx.xxx:xxxx/nexus/content/repositories</url>
                <releases>
                    <enabled>true</enabled>
                    <updatePolicy>always</updatePolicy>
                    <checksumPolicy>warn</checksumPolicy>
                </releases>
                <snapshots>
                  	<enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
    </profile>
  	<profile>
        <id>home</id>
        <repositories>
            <repository>
                <id>aliyun</id>
                <name>central</name>
                <url>https://maven.aliyun.com/repository/central</url>
                <releases>
                    <enabled>true</enabled>
                    <updatePolicy>always</updatePolicy>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                    <updatePolicy>always</updatePolicy>
                </snapshots>
            </repository>
        </repositories>
    </profile>
		<activeProfiles>
        <activeProfile>company</activeProfile>
        <activeProfile>home</activeProfile>
  </activeProfiles>
</settings>

After configuration, the idea will display an optional box in the sidebar. Click the corresponding office environment and pack again.
As shown below:

2. Build log output configuration skills

When packaging, we will notice that the build log will be output to the screen. If the project is large, a large number of logs will also be output. Is there any way to adjust the output log level? The answer is yes. Please refer to the following three schemes:

  1. Edit the mvn execution file ${MAVEN_HOME}/bin/mvn and add a new line of configuration
MAVEN_OPTS="-Dorg.slf4j.simpleLogger.defaultLogLevel=warn"
  1. maven installation directory: conf / logging / simplelogger Properties modify the output log level
org.slf4j.simpleLogger.defaultLogLevel=warn
  1. The - q parameter is used when executing the mvn command, but only error information can be output
mvn clean package -DskipTests -q

3. File filtering and packaging configuration skills

Sometimes we want maven to package only some resource files into the jar package, not all files. At this time, we can configure the POM file as follows:

<!--to configure Maven yes resource File filtering -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- When packaging, only load aa.properties and bb.xml -->
                <includes>
                    <include>**/aa.properties</include>
                    <include>**/bb.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>

4. Configuration skills for offline development

Sometimes for security reasons, we need to develop in a disconnected environment, such as a production environment. You cannot connect to the Internet at this time. How to load the dependency? How to develop? maven provides offline mode. The premise is to upload the downloaded dependency package to the Intranet environment and install maven and other related tools. Please refer to the following two schemes:

  1. In settings Make changes in XML and add in the first tag

<localRepository>~/.m2/repository</localRepository>
<offline>true</offline>

Use mvn clean package -DskipTests=true to package

  1. Specify the local warehouse in the pom file

	<repositories>
    <repository>
      <id>local</id>
      <name>local Repository</name>
      <url>file://${project.basedir}/.m2/repository</url>
    </repository>
  </repositories>

Use mvn -o clean install -DskipTests=true to package

3, Summary

The above is what big lion brother shared with you today about maven configuration, including the configuration of multi office environment switching, the configuration of building log output, the configuration of packaging file filtering and the configuration of offline development. If there are any mistakes in the article's views or conclusions or others, please comment or correct them in private letters. At the same time, if this article is helpful to you, you are also welcome to praise, collect and pay attention.

Keywords: Java Maven IDE

Added by amax on Mon, 03 Jan 2022 21:05:02 +0200