Maven和Nexus 打成jar包 上传jar包

依赖查询网
下载地址

一。Maven安装

1.下载解压  apache-maven-3.3.9-bin.zip
2.配置环境变量:  D:\maven\apache-maven-3.6.3\bin   查看版本检测是否成功:mvn -version
3.配置本地仓库  在setting.xml    <localRepository>D:\maven\apache-maven-3.6.3\cangku</localRepository>
4.配置私服(镜像)  在setting.xml
	 <mirror>
		<id>alimaven</id>
		<name>aliyun maven</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
		<mirrorOf>central</mirrorOf>       
	</mirror> 
5.MyEclipse关联Maven

mvn clean package install -Dmaven.test.skip=true  -s /usr/local/maven/config/setting.xml 
                             跳过测试                   指定配置文件

二。POM配置

a、基础元素配置:
b、全局属性配置:
c、依赖关系配置:
d、构建元素配置:
e、常用插件配置
f、站点元素配置:

a、基础元素配置:
    基础元素配置
    
   <!-- 指定POM对象模型的版本号 -->
   <modelVersion>4.0.0</modelVersion>
   
   <!-- 指定组织ID(公司域名反写) -->
   <groupId>cn.itcast</groupId>
   
   <!-- 指定项目名 -->
   <artifactId>Struts2-MyBatis3</artifactId>
   
   <!-- 指定项目的版本  RELEASE(发行版)SNAPSHOT(快照版) -->
   <version>1.0.0-RELEASE</version>
   version>${revision}</version>      //在全局属性配置<properties>里面有个  <revision>1.0.0.BUILD-SNAPSHOT</revision>
   
   <!-- 指定项目打包的产物(jar、ear、war、pom) -->
   <packaging>war</packaging>
b、全局属性配置:
 <properties>
	  <currentVersion>${project.version}</currentVersion>
	  <struts2.version>2.3.16.3</struts2.version>
	  <mybatis.version>3.2.7</mybatis.version>
	  <mysql.version>5.1.27</mysql.version>
	  <tomcat.version>7.0.47</tomcat.version>
 </properties>
c、依赖关系配置:
DepencyManagement 父类工程管理机制
 <dependencies>  
       <dependency>
	     <groupId>jstl</groupId>
	     <artifactId>jstl</artifactId>
	     <version>1.2</version>
	     <scope>runtime</scope>
	     <type>jar</type>
	     <classifier>jdk15</classifier>
       </dependency>
 </dependencies>
和properties   dependencies同级别
<profiles>
        <profile>
            <!-- 本地开发环境 -->
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <activation>
                <!-- 设置默认激活这个配置 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 发布环境 -->
            <id>product</id>
            <properties>
                <profiles.active>product</profiles.active>
            </properties>
        </profile>
        <profile>
            <!-- 测试环境 -->
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>

        </profile>
    </profiles>
d、构建元素配置:
 <build>
    <finalName>hrm</finalName>
    <!-- 定义默认的目标 -->
    <defaultGoal>package</defaultGoal>
    <!-- 源码目录 -->
    <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
    <!-- 测试代码目录 -->
    <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
    <!-- 资源文件 -->
    <resources>
	<resource>
	     <!-- 源目录 -->
	     <directory>${basedir}/src/main/resources</directory>
	</resource>  
    </resources>
    <plugins>
       <plugin>
       </plugin>
    </plugins>
</build>
e、常用插件配置
1. 编译插件: 
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>3.1</version>
   <configuration>
   <source>1.7</source>
   <target>1.7</target>
   <encoding>utf-8</encoding>
   </configuration>  
</plugin>
    
2. 生成API文档插件
<plugin>
   <groupId>org.apache.maven.plugins</groupId>  
   <artifactId>maven-javadoc-plugin</artifactId>
   <version>2.9.1</version>
   <!-- 指定插件需要的配置信息 -->
   <configuration>
   <!-- 源文件编码 -->
   <encoding>UTF-8</encoding> 
   <!-- 生成时的编码 -->
   <charset>UTF-8</charset> 
   <!-- 文档的编码 -->
   <docencoding>UTF-8</docencoding>
   <!-- 文档标题 -->
   <doctitle>传智播客</doctitle>
   <!-- 窗口标题 -->
   <windowtitle>OA办公管理系统</windowtitle>
   </configuration>
   <executions>
        <execution>
              <!-- 指定目标 -->
              <goals><goal>javadoc</goal></goals>
              <!-- 生命周期哪个阶段时,执行上面javadoc目标 -->
         <phase>compile</phase>
        </execution>
   </executions>  
</plugin>

3. tomcat插件:
<plugin>
   <groupId>org.apache.tomcat.maven</groupId>
   <artifactId>tomcat7-maven-plugin</artifactId>
   <version>2.2</version>
   <!-- 指定插件需要的配置信息 -->
   <configuration> 
	 <!-- 设置端口 -->
	 <port>8080</port>
	 <!-- 设置支持热部署 -->
	 <contextReloadable>true</contextReloadable>
   </configuration>   
</plugin> 
	   
4. jetty插件:
<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>7.6.16.v20140903</version>
    <configuration>
        <!-- 设置web项目配置信息 -->
        <webApp>
           <contextPath>/${project.artifactId}</contextPath>
        </webApp>
        <!-- 设置支持热部署 -->
        <scanIntervalSeconds>1</scanIntervalSeconds>
             <connectors>
                     <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                         <port>9090</port>
                     </connector>
             </connectors>       
   </configuration>
</plugin>

 f、站点元素配置:
 <!-- 指定项目名称,一般用于文档生成 -->
 <name>ssh</name>
 <!-- 指定公司网站地址 -->
 <url>http://maven.apache.org</url>
 <!-- 指定项目描述 -->
 <description>传智播客java架构项目</description>
 <!-- 指定项目的开始时间 -->
 <inceptionYear>2015</inceptionYear>

三。POM继承他人parent

<parent>
    <artifactId>cloud-zero</artifactId>
    <groupId>com.wind</groupId>
    <version>1.0.0</version>
</parent>

四。POM拥有api模块的bean 接口

在dependencie标签添加依赖(信息取自API模块的POM<dependency>
        <groupId></groupId>
        <artifactId>spring-boot-starter-web</artifactId>
   </dependency>

五。项目打成jar包并给其他项目使用

a项目打成jar包到本地仓库

方法1:点击Install就在项目目录下的target文件夹下添加了这个项目的jar包
方法2:进入项目的目录然后命令:mvn clean package -Dmaven.test.skip=true   后面那个表示跳过test文件夹的打包
       -pl 选项后可跟随{groupId}:{artifactId}或者所选模块的相对路径(多个模块以逗号分隔)
       -am 表示同时处理选定模块所依赖的模块
       
idea package和install的区别?
package和install都会把项目打包成jar/war存到target目录,
但是install还会在maven目录生成jar包
   
如果要打war包,在pom文件像如下那样写
  <!-- 指定项目打包的产物(jar、ear、war、pom) -->
   <packaging>war</packaging>

运行项目:进入项目的target文件夹,java -jar xxx.jar 
 JNI参数: -Djava.library.path=/home/project/substationserver/jni
 内存溢出   -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/usr/local/base

在这里插入图片描述
在这里插入图片描述

如果有私服,还要上传
在这里插入图片描述

b项目引用a项目打成的jar包

1.添加依赖

<dependency>
    <groupId>com.lhc</groupId>
    <artifactId>SpringBootDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
repositories {
    mavenLocal()     <--从本地仓库获取 ->>
    maven { url "http://47.96.230.188:8081/repository/Public_Repositories/" }    <--从本地仓库获取 ->>
}

2。刷新依赖
在这里插入图片描述

六。nexus私服

1.。私服说明: 私服是一种特殊的远程仓库,它是架设在局域网内的仓库服务

1.用户使用Maven构建项目时,首先是要直接从本地仓库获取的,
2.如果本地仓库没有,它会根据setting.xml的设置去首先尝试从远程仓库下载构件至本地仓库,然后再使用本地仓库的构件。
  如果setting.xml设置的远程仓库是本地代理仓库(即私服),则本地代理仓库先尝试从自己(私服)的库中获取,
  如果没有再从远程仓库(比如中央仓库)下载构件至本地仓库,然后再使用本地仓库的构件。

2。搭建nexus私服服务
官网下载地址
百度网盘下载地址

1.下载并解压nexus-2.10.0-02-bundle.zip,
  nexus-2.10.0-02  核心目录
  sonatype-work    工作目录   D:\nexus\sonatype-work\nexus\storage:所有参考存储目录

2.配置环境变量D:\nexus\nexus-3.6.0-02\bin
   并进入bin目录CMD敲命令:nexus /run  进行运行安装   
 
3.访问
  http://localhost:8081   账号:admin   密码:admin123

4.页面介绍
    三个仓库类型: hosted(宿主仓库)  proxy(代理仓库)    group(仓库组)
    四个已有仓库

5.  添加仓库:见下面截图         

在这里插入图片描述
3、上传jar包、项目到nexus

一。上传jar包(测试没成功)
CMD输入命令
mvn deploy:deploy-file 
	-DgroupId=com.lhc
	-DartifactId=SpringBootDemo
	-Dversion=1.0-SNAPSHOT
	-Dpackaging=jar 
	-Dfile=E:\smallfat\SpringBootDemo-1.0-SNAPSHOT.jar
	-Durl=http://localhost:8081/repository/test/
	-DrepositoryId=test

-DgroupId 为上传的jar的groupId
-DartifactId 为上传的jar的artifactId
-Dversion 为上传的jar的需要被依赖的时候的版本号
-Dpackaging为jar
-Dfile为jar包路径, 最好就在D盘的根目录D:\xxx.jar, 不要D:\xxx\xxx\xxx\xxx.jar,这样可能会报错
-Durl 为要上传的路径,
-DrepositoryId 为repository的唯一标示,跟第3步中权限配置的server相同
二。上传项目到nexus(测试成功)
a.maven的settings.xml文件添加如下内容
     <server>
      <id>test</id>                   nexus上仓库的名称(具有唯一性)
      <username>admin</username>      nexus账号
      <password>admin123</password>   nexus密码
    </server>
    
b.在项目的Pom添加如下(和dependencies属于同一级别,在project级别下)
	<distributionManagement>  
	     <repository>  
	         <id>nexus</id>                                                    nexus上仓库的名称(具有唯一性)                                              
	         <url>http://192.168.10.68:8081/repository/maven-releases/</url>   nexus上仓库的
	     </repository>  
	</distributionManagement> 
	 注:id为要上传的repository的唯一标示,url为要上传的repository的路径
	 
c.cmd到项目的pom.xml文件目录中运行:mvn deploy即可
   或者点击 maven——SpringBootDemo——Lifecycle——deploy

4。从私服下载jar到本地仓库

在maven的conf/settings.xml文件
<mirror>
   <id>public</id>
   <mirrorOf>*</mirrorOf>
   <name>public Mirror</name>
   <url>http://localhost:8081/nexus/content/groups/public</url>
</mirror>

id : 镜像唯一标识符,用来区分不同的mirror
mirrorOf : 镜像哪些仓库 *:代表所有的仓库 central: 代表仓库id为central的仓库.
name : 镜像名称
url : 镜像的URL
一段比较完整的远程仓库的配置
<repositories>  
        <repository>  
            <id>jboss</id>  
            <name>JBoss Repository</name>  
            <url>http://repository.jboss.com/maven2/</url>  
            <releases>  
                <updatePolicy>daily</updatePolicy><!-- never,always,interval n -->  
                <enabled>true</enabled>  
                <checksumPolicy>warn</checksumPolicy><!-- fail,ignore -->  
            </releases>  
            <snapshots>  
                <enabled>false</enabled>  
            </snapshots>  
            <layout>default</layout>  
        </repository>  
</repositories>

maven打成jar包并用CMD运行

1.maven————Lifecycle————clear————package,然后项目的目录下的target文件夹下多了
    ExceptionOrderData-1.0-SNAPSHOT.jar    ExceptionOrderData-1.0-SNAPSHOT.jar.original

2.cmd命令  java -jar ExceptionOrderData-1.0-SNAPSHOT.jar
3.post访问服务测试

如果CMD java -jar 出现ExceptionOrderData-1.0-SNAPSHOT.jar中没有主清单属性,pom添加如下
 <build>
    <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.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>


七。maven设置编译包含springboot项目resource下的文件夹下的文件

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
                <includes>
                   <include>*</include>   //要有这个,不然第一层目录的文件都没有嘞
                    <include>certs/*.*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
<!--                <configuration>-->
<!--                    <includeSystemScope>true</includeSystemScope>-->
<!--                </configuration>-->
            </plugin>
        </plugins>
    </build>

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飘然生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值