Build your own maven project

     Manually create a maven project yourself. First of all, you must understand the directory structure of the maven project, and secondly, you must be able to configure pom.xml.

      Let’s illustrate

        Prerequisite: Maven configuration is successful

   Single maven project

        1 Manually create a directory. You can put the directory there. You can take the file name by yourself. Here I named it backup1, then open the directory, create a directory named src in the directory, and create a pom.xml file

  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>backup</groupId>
  <artifactId>backup</artifactId>
   <version>1.0</version>
  <packaging>jar</packaging>
  <name/>
  <description/>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.4</version>
  </dependency>
  </dependencies>
  <build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

The pom.xml file is standard and cannot be dropped.

  <groupId>backup</groupId>
  <artifactId>backup</artifactId>

These two are required, one for group id and one for project id

<packaging>jar</packaging> This indicates the type of packaging. There are jar, war, ear, pom, the first three are common, and the last one means there are sub-maven directories,

  

 <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>2.4</version>
  </dependency>

This is which package you need, you import which package, you need to know the groupid, artifactid, version of the imported package. What I introduced is servlet, it will find servlet-api in remote library or import it in local warehouse.


Next is the Maven plug-in. Maven is essentially a plug-in framework. Its core does not perform any specific build tasks. All these tasks are completed by the plug-in. For example, compiling the source code is done by the maven-compiler-plugin. , So to compile, you need to configure a <artifactId>maven-compiler-plugin</artifactId>, and a maven-compiler-plugin code. If you want to have fun with maven, here is the entry point, but I only know a little For one thing, it has not been studied in depth.

配置基本上,完成了,在进入src目录,在创建一个main目录,在目录下在创建三个目录,如下,java目录就是写java代码的地方了,resource就是放配置文件的地方了,webapp就是web项目。


   好了一个空的maven工程就好了,

    在backup目录下执行mvn package , 执行完毕后,你在src目录下就多了一个target目录,里面就是打包jar,backup-1.0.jar就出来了。

    如果你在pom.xml中把<packaging>jar</packaging>,改为<packaging>war</packaging>,打包出来就是一个war包了。

   maven项目有多子项目创建

      在创建一个目录backup,拷贝上面已经创建好了的项目,在复制一份,改个名字backup2, 在复制一个名为backup3,在backup目录下创建一个pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.backup</groupId>
  <artifactId>backup</artifactId>
   <version>1.0</version>
  <packaging>pom</packaging>
  <name/>
  <description/>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <modules>
<module>backup1</module>
<module>backup2</module>

<module>backup3</module>

</modules>
  <build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

 这个pom.xml不同之处有1  packaging 值为pom,2 多了modules组件,这就是可以引入多个maven项目,这里配置了3个maven项目

 backup下的3个maven项目pom有变,

 <parent>
<groupId>com.backup</groupId>
<artifactId>backup</artifactId>
<version>1.0</version>
</parent>
三个pom都需要加入parent节点 ,这样表明三个maven同在一个组,backup1和backup2设置  <packaging>jar</packaging>

儿backup3 <packaging>war</packaging>同时依赖backup1,backup2

<dependencies>
    <dependency>
  <groupId>com.backup</groupId>
  <artifactId>backup1</artifactId>
  <version>1.0</version>
  </dependency>
 <dependency>
  <groupId>com.backup</groupId>
  <artifactId>backup2</artifactId>
  <version>1.0</version>
  </dependency>

  </dependencies>

这样在backup目录下打包,在backup3/target,就可以看到刚打好的包。在lib中会引入backup1,backup2两个jar包的。

多个maven项目依赖就是这样玩的了。我还玩的不够熟练,只能简单的介绍到这里了。


 


 





      

Guess you like

Origin blog.csdn.net/samz5906/article/details/43412111