如何创建一个"管理jar包版本号"的maven工程呢?

乔欣 Maven教程 发布时间:2022-11-17 09:53:14 阅读数:17615 1
下文笔者讲述创建一个maven父工程-管理版本号的方法分享,如下所示
在springboot项目中,我们经常看见一个所有的Springboot项目都指向一个父项目
那么这个父项目到底是一个什么东西呢?下文笔者将一一道来,如下所示
我们可通过建立一个maven工程,并设置其项目类型为pom
    这个pom项目可用于管理依赖及版本号
	  那么子项目就可以使用定义的依赖
	  子项目依赖jar包时,无需指定相应的版本号
	  即可直接使用jar包
例:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
 
<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.java265.test</groupId>
    <artifactId>testDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
 
    <name>testDemo</name> 
    <url>http://www.java265.com</url>
 
    <!-- 集中定义依赖版本号 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.11</junit.version>
        <slf4j.version>1.7.25</slf4j.version>
        <logback.version>1.2.3</logback.version>
        <logback.ext.spring.version>0.1.4</logback.ext.spring.version>
        <log4j.version>2.4.1</log4j.version>
        <fastjson.version>1.2.47</fastjson.version>
        <jackson.version>2.9.2</jackson.version>
        <httpclient.version>4.5.10</httpclient.version>
        <jdom.version>1.0</jdom.version>
        <spring.version>4.3.16.RELEASE</spring.version>
        <lombok.version>1.18.6</lombok.version>
        <jodatime.version>2.3</jodatime.version>
        <curator.version>2.4.1</curator.version>
        <disruptor.version>3.2.0</disruptor.version>
        <mysql.version>5.1.38</mysql.version>
        <quartz.version>2.3.0</quartz.version>
        <activemq.client.version>5.15.10</activemq.client.version>
        <aspectj.version>1.8.10</aspectj.version>
        <jna.version>4.4.0</jna.version>
        <commons.lang3.version>3.8</commons.lang3.version>
        <aopalliance.version>1.0</aopalliance.version>
    </properties>
 
    <!-- 管理jar包,不会引入 ,如果子工程需要哪些jar包,则具体地在子工程中引入,不过不需要写版本号-->
    <dependencyManagement>
        <dependencies>
            <!-- 单元测试 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <!-- log-->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>${logback.version}</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>${logback.version}</version>
            </dependency>
            <dependency>
                <groupId>org.logback-extensions</groupId>
                <artifactId>logback-ext-spring</artifactId>
                <version>${logback.ext.spring.version}</version>
            </dependency>
 
            <!--jackson-->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>${jackson.version}</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>${jackson.version}</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${jackson.version}</version>
            </dependency>
 
            <!--fastjson-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>
 
            <!--joda -->
            <dependency>
                <groupId>joda-time</groupId>
                <artifactId>joda-time</artifactId>
                <version>${jodatime.version}</version>
            </dependency>
 
            <!--httpclient -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>${httpclient.version}</version>
            </dependency>
 
            <!--jdom-->
            <dependency>
                <groupId>jdom</groupId>
                <artifactId>jdom</artifactId>
                <version>${jdom.version}</version>
            </dependency>
 
            <!--activemq-->
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-client</artifactId>
                <version>${activemq.client.version}</version>
            </dependency>
 
            <!--spring-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!--spring aop-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>aopalliance</groupId>
                <artifactId>aopalliance</artifactId>
                <version>${aopalliance.version}</version>
            </dependency>
            <!--spring transactional-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!--spring web RestTemplet Dependency-->
            <!--        <dependency>-->
            <!--            <groupId>org.springframework</groupId>-->
            <!--            <artifactId>spring-web</artifactId>-->
            <!--            <version>${spring.version}</version>-->
            <!--        </dependency>-->
 
            <!--aspectj-->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectj.version}</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>${aspectj.version}</version>
            </dependency>
 
            <!--tool-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
            <dependency>
                <groupId>net.java.dev.jna</groupId>
                <artifactId>jna</artifactId>
                <version>${jna.version}</version>
            </dependency>
            <dependency>
                <groupId>net.java.dev.jna</groupId>
                <artifactId>jna-platform</artifactId>
                <version>${jna.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>${commons.lang3.version}</version>
            </dependency>
 
        </dependencies>
    </dependencyManagement>
 
    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
 
    <distributionManagement>
        <repository>
            <!--id的名字可以任意取,但是在setting文件中的属性<server>的ID与这里一致-->
            <id>releases</id>
            <!--指向仓库类型为host(宿主仓库)的储存类型为Release的仓库-->
            <url>http://10.20.30.158:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <!--指向仓库类型为host(宿主仓库)的储存类型为Snapshot的仓库-->
            <url>http://10.20.30.158:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
</project>

将父工程install到本地或者deploy到maven私服


新创建一个maven工程
在pom.xml中
继承父工程
依赖去掉版本号
<?xml version="1.0" encoding="UTF-8"?>
 
<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>
 
    <parent>
        <groupId>com.kexi.hmi.pictureeditor</groupId>
        <artifactId>pictureeditor-core-environment</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
 
    <groupId>com.kexi.hmi.pictureeditor</groupId>
    <artifactId>pictureeditor-core-gdiclient</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <name>gdiclient</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <slf4j.version>1.7.25</slf4j.version>
        <logback.version>1.2.3</logback.version>
        <fastjson.version>1.2.41</fastjson.version>
        <jackson.version>2.9.2</jackson.version>
        <httpclient.version>4.5.10</httpclient.version>
        <jdom.version>1.0</jdom.version>
        <spring.version>4.3.16.RELEASE</spring.version>
        <lombok.version>1.18.6</lombok.version>
        <jodatime.version>2.3</jodatime.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
 
        <!-- logback start -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <dependency>
            <groupId>org.logback-extensions</groupId>
            <artifactId>logback-ext-spring</artifactId>
        </dependency>
        <!--log桥接工具-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>
        <!-- logback end -->
 
        <!--fastjson start-->
        <!--    <dependency>-->
        <!--      <groupId>com.alibaba</groupId>-->
        <!--      <artifactId>fastjson</artifactId>-->
        <!--      <version>${fastjson.version}</version>-->
        <!--    </dependency>-->
        <!--fastjson end-->
 
        <!--jackson start-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <!--jackson end-->
 
        <!--fastjson start-->
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <!--fastjson end-->
 
        <!--joda start-->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>
        <!--joda end-->
 
        <!--httpclient start-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <!--httpclient end-->
 
        <!--jdom start-->
        <dependency>
            <groupId>jdom</groupId>
            <artifactId>jdom</artifactId>
        </dependency>
        <!--jdom end-->
 
        <!--spring core start-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <!--spring core end-->
 
        <!--spring aop start-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
        </dependency>
        <!--spring aop end-->
 
        <!--spring transactional start-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <!--spring transactional end-->
        <!--spring web RestTemplet Dependency start-->
        <!--        <dependency>-->
        <!--            <groupId>org.springframework</groupId>-->
        <!--            <artifactId>spring-web</artifactId>-->
        <!--            <version>${spring.version}</version>-->
        <!--        </dependency>-->
        <!--spring web RestTemplet Dependency end-->
 
        <!--tool start-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna-platform</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <!--tool end-->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-client</artifactId>
        </dependency>
    </dependencies>
 
    <build>
        <defaultGoal>compile</defaultGoal>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
                <excludes>
                    <exclude>**/.svn/*</exclude>
                </excludes>
                <filtering>false</filtering>
            </resource>
        </resources>
 
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.5</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.kexi.hmi.pictureeditor.core.gdiclient.GDIExample</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <!-- 以下是将项目依赖打到一个包里-->
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/lib</outputDirectory>
                            <overWriteSnapshots>true</overWriteSnapshots>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
 
    <!--上传到nexus仓库中,配合mvn deploy:deploy-->
    <!--    <distributionManagement>-->
    <!--        <repository>-->
    <!--            <id>nexus</id>-->
    <!--            <name>Nexus snapshots Repository</name>-->
    <!--            <!–snapshots仓库 –>-->
    <!--            <url>http://127.0.0.1:8081/repository/maven-snapshots/</url>-->
    <!--        </repository>-->
    <!--    </distributionManagement>-->
 
    <!--    <distributionManagement>-->
    <!--        <snapshotRepository>-->
    <!--            <id>maven-snapshots</id>-->
    <!--            <name>User Porject Snapshot</name>-->
    <!--            <url>http://127.0.0.1:8081/repository/maven-snapshots/</url>-->
    <!--            <uniqueVersion>true</uniqueVersion>-->
    <!--        </snapshotRepository>-->
    <!--        <repository>-->
    <!--            <id>maven-releases</id>-->
    <!--            <name>User Porject Release</name>-->
    <!--            <url>http://127.0.0.1:8081/repository/maven-releases/</url>-->
    <!--        </repository>-->
    <!--    </distributionManagement>-->
 
    <distributionManagement>
        <repository>
            <!--id的名字可以任意取,但是在setting文件中的属性<server>的ID与这里一致-->
            <id>releases</id>
            <!--指向仓库类型为host(宿主仓库)的储存类型为Release的仓库-->
            <url>http://192.168.8.8:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <!--指向仓库类型为host(宿主仓库)的储存类型为Snapshot的仓库-->
            <url>http://192.168.8.8:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
</project>

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/Maven/202211/4933.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者