Maven手动构建jar包

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Andyzhu_2005/article/details/82774503

何为maven

maven是一个纯java开发的,用来管理java项目的。其用处:
1、依赖管理,即管理jar包。普通的项目,需要将项目所需的jar包放入libs文件夹中。而maven项目,只需要通过pom文件即可添加和管理jar包。
2、用于项目的构建。对于一个普通的只包含java源代码的项目,可以通过maven命令,完成项目的编译、测试、运行、打包以及部署等。

Maven的目录结构

在这里插入图片描述

Maven构建命令

mvn clean: 清理编译的文件,将target目录删除
mvn compile: 编译源码
mvn package:打包
mvn test:编译并运行test目录下的代码
mvn install: 安装打包文件到仓库
mvn tomcat:run 启动tomcat服务器

#maven打包可执行的jar文件
##没有引用第三方jar包,在pom添加插件

	<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib/</classpathPrefix>
							<!-- 程序入口 -->
							<mainClass>jarTest.JavaTest</mainClass>
						</manifest>
					</archive>
				</configuration>
			</plugin>

在cmd中,进入项目pom文件所在目录,执行
mvn clean
mvn package
进入到target目录,执行可执行程序
java -jar jarTest-0.0.1-SNAPSHOT.jar

引用了其他jar包的情况(第三方jar包不与package打成一个包)

比如,在项目中引用了netty的jar包,

<dependencies>
		<dependency>
			<groupId>io.netty</groupId>
			<artifactId>netty-all</artifactId>
			<version>5.0.0.Alpha1</version>
		</dependency>
	</dependencies>

如果仍用上述方法,会报找不到netty包的方法的错误。通过在pom文件中添加如下插件

	<build>
		<plugins>


			<!-- 设置编译版本为1.7 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>




			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib/</classpathPrefix>
							<mainClass>jarTest.JavaTest</mainClass>
						</manifest>
					</archive>
				</configuration>
			</plugin>

		  <!-- 拷贝依赖的jar包到lib目录 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!-- ${project.build.directory}是maven变量,内置的,表示target目录,如果不写,将在跟目录下创建/lib -->
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <!-- excludeTransitive:是否不包含间接依赖包,比如我们依赖A,但是A又依赖了B,我们是否也要把B打进去 默认不打-->
                            <excludeTransitive>true</excludeTransitive>
                            <!-- 复制的jar文件去掉版本信息,最好设置为false -->
                            <stripVersion>false</stripVersion>
                        </configuration>
                    </execution>
                </executions>
			</plugin>

		</plugins>
	</build>

引用了其他jar包的情况(第三方jar包与package打成一个包)

<build>
		<plugins>
			<!-- 设置编译版本为1.7 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>jarTest.JavaTest</mainClass>
                                </transformer>
                                <!--<transformer-->
                                        <!--implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">-->
                                    <!--<resource>applicationContext.xml</resource>-->
                                <!--</transformer>-->
                            </transformers>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <shadedClassifierName>executable</shadedClassifierName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


		</plugins>
	</build>

maven依赖范围

依赖范围通过 标签来确定。

<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
			<scope>provider</scope>
		</dependency>

其值可分为以下几种:
1、compile :从编译、测试、打包、运行都需要
2、provided:编译、测试是需要,打包、运行不需要jar包(经常servlet-api和jsp-api会用到这个,因为tomcat服务器里有此两个jar,因此我们在打包时候不需要将这两个包打进去)
3、runtimer:编译时候不需要,测试、打包、运行时候需要
4、test: 只有测试时候才需要(junit.jar)

maven对于jar包冲突的处理

1、排除原则 ,通过exclusion标签

	<!-- dubbo相关 -->
   	<dependency>
   		<groupId>com.alibaba</groupId>
   		<artifactId>dubbo</artifactId>
   		<exclusions>
   			<exclusion>
   				<groupId>org.springframework</groupId>
   				<artifactId>spring</artifactId>
   			</exclusion>		        
   	     	<exclusion>
   				<groupId>org.jboss.netty</groupId>
   				<artifactId>netty</artifactId>
   			</exclusion>
   		</exclusions>
   	</dependency>

2、版本锁定。通常在父工程中通过dependencyManagement 统一多模块的依赖版本

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				<version>${spring.version}</version>
			</dependency>
</dependencies>
</dependencyManagement>

猜你喜欢

转载自blog.csdn.net/Andyzhu_2005/article/details/82774503