The first blog---- share some skills about the use of Maven

Maven environment setup

Download the maven installation package on the official website, address: http://maven.apache.org/download.cgi .
Unzip the file to the computer's drive letter directory, such as E:\apache-maven-3.3.9-bin.
Set the environment variable, create a new MAVEN_HOME, and then copy the maven path into the variable value.
Add the ;%MAVEN_HOME%\bin path to the path. Enter mvn -v in the dos interface to check whether the environment and maven version are successfully configured.

After the environment variables are configured successfully, you need to do the following configuration:
1. Create a folder MavenRepository locally, and create a folder repo under the MavenRepository folder.
2. Enter the unzipped Maven folder, enter the conf folder, and copy the settings.xml file to the MavenRepository
folder created in the previous step.
3. Open the settings.xml file in the MavenRepository folder and find the localRepository label, which is now If it is commented out, we
uncomment it, and then configure the repo path in step 1, such as
input: mvn help:system to see if the changed setting configuration takes effect

use maven in eclipse

Install the maven plugin

In eclipse, you need to download the maven offline plug-in package. After the successful download, unzip it, import the jar package files of the subfolders features and plugins into the features and plugins folders in the Eclipse installation directory, and then restart Eclipse to see it in Preferences to the Maven options. That is, the Maven plugin is configured successfully.

Note: If MyEclipse does not need to perform the above steps, MyEclipse comes with a Maven plug-in.

configure maven

Search for maven in Perferences in Eclipse, click Installations, then click add, and select the path of maven to load.
After loading, select maven's settings.xml file in User Settings, and then click ok.
After the configuration is complete, open File->New->others, search for maven, and if you see a Maven Project, it means the configuration is successful.

Create maven project

Create with eclipse

Open File->New->others, search for maven, select Maven Project, and click Create. The operation is shown in the following figure:

Create with maven command

  1. Create a common Java project for Maven and enter under the dos command: 
    mvn archetype:generate -DgroupId=packageName -DartifactId=projectName 
  2. Create a Maven Web project and enter under the dos command: 
    mvn archetype:generate -DgroupId=packageName -DartifactId=webappName-DarchetypeArtifactId=maven-archetype-webapp 

Some tricks used by maven

maven set default JDK

If the default jdk is not set, the projects created and imported are generally jdk1.4, which needs to be changed manually, so here we set the default jdk in the maven configuration.
Add in setting.xml

<!--  设置maven默认jdk -->
   <profile>  
    <id>jdk-1.7</id>  
    <activation>  
        <activeByDefault>true</activeByDefault>  
        <jdk>1.7</jdk>  
    </activation>  
    <properties>  
        <maven.compiler.source>1.7</maven.compiler.source>  
        <maven.compiler.target>1.7</maven.compiler.target>  
        <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>  
    </properties>  
</profile>

Set up Ali's private server

Due to network limitations, it is very slow to download jar packages using maven's default address, so it is faster to use Ali's maven server.

in setting.xml

    <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>        
    </mirror>

Package with maven

If you use maven package, just add in pom.xml

<build>  
<resources>  
            <resource>  
                <directory>src/main/resource</directory>  
                <includes>  
                    <include>*.properties</include>  
                    <include>*.xml</include>
                    <include>*.tld</include>
                </includes>  
                <filtering>false</filtering>  
            </resource>
</resources>
<defaultGoal>compile</defaultGoal>
        <sourceDirectory>src</sourceDirectory>
        <finalName>first</finalName>
    <plugins>  
        <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-assembly-plugin</artifactId>  
                <version>2.5.5</version>  
                <configuration>  
                    <appendAssemblyId>false</appendAssemblyId>  
                    <descriptorRefs>  
                        <descriptorRef>jar-with-dependencies</descriptorRef>  
                    </descriptorRefs>  
                    <archive>  
                        <manifest>  
                            <mainClass>com.test.App</mainClass>  
                        </manifest>  
                    </archive>  
                </configuration>  
                <executions>  
                    <execution>  
                        <id>make-assembly</id>  
                        <phase>package</phase>  
                        <goals>  
                            <goal>assembly</goal>  
                        </goals>  
                    </execution>  
                </executions>  
            </plugin>   
  
    </plugins>  
</build> 

Description: The above packaging is to put all maven dependencies and resources into a runnable jar and specify a main method.

Then go to the following directory of the project, which is the same directory as pom.xml, open the dos interface, and enter mvn package to package it.

Some problems encountered with maven

1. Using eclipse to package the project appears: Dmaven.multiModuleProjectDirectory system property is not set.

    1添加M2_HOME的环境变量

  2.Preference->Java->Installed JREs->Edit select a jdk,
  add -Dmaven.multiModuleProjectDirectory=$M2_HOME to the default vm argument

2. The diamond operator is not supported in source-1.5

Solution 1:
Add in properties

<maven.compiler.source>1.8</maven.compiler.source>和<maven.compiler.target>1.8</maven.compiler.target>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

Solution two:
add 1.8 and

<build>  
        <plugins>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>3.3</version>  
                <configuration>  
                    <source>1.8</source>  
                    <target>1.8</target>  
                </configuration>  
            </plugin>  
        </plugins>  
    </build>  

3. A jar package cannot be entered in the packaging

It can actually be downloaded, but I can't put this into the package

Solution:
add in pom.xml

  <repositories>  
     <repository>  
            <id>spring-milestone</id>  
            <url>http://repo.spring.io/libs-release</url>  
     </repository>  
   </repositories> 

Epilogue

This is a blog of mine, I don't know what to write after thinking about it. If so, the previous notes will be sorted out and sent out. Because it is the first time to write a blog, many of them do not understand, if the writing is not very good, please forgive me!

Let me say two more words! After visiting the blog garden and CSDN and other websites often, I found that many people are writing blogs, and I think they are amazing! I was still a novice, and I didn't have the idea of ​​blogging at that time. Until later, I read a lot of blogs, and I also read some so-called Xiaobai who are also writing blogs. Among them, I also read some great gods' suggestions for blogging. There is a sentence in one of the blogs that I think is very useful. The best time to write a blog is in the past, followed by the present, and then in the future! So I feel that it is urgent to write now, so I have this article!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325080841&siteId=291194637