代码改变世界

maven常用命令 maven中下载jar包源码和javadoc

2016-03-25 12:04  youxin  阅读(4807)  评论(0编辑  收藏  举报

1:Maven命令下载源码和javadocs

当在IDE中使用Maven时如果想要看引用的jar包中类的源码和javadoc需要通过maven命令下载这些源码,然后再进行引入,通过mvn命令能够容易的达到这个目的:

 

mvn dependency:sources
mvn dependency:resolve -Dclassifier=javadoc

命令使用方法:首先进入到相应的pom.xml目录中,然后执行以上命令:

 

第一个命令是尝试下载在pom.xml中依赖的文件的源代码。

第二个命令:是尝试下载对应的javadocs

但是有可能一些文件没有源代码或者javadocs

reference

Executing mvn dependency:sources will force maven to download all sources of all jars in the project, if the sources are available (are uploaded in the repository where the artifact is hosted). If you want to download javadoc the command is mvn dependency:resolve -Dclassifier=javadoc

http://stackoverflow.com/questions/11361331/how-to-download-sources-for-a-jar-with-maven

2:通过配置文件添加

打开maven配置文件 setting.xml文件(.../.m2/settings.xml) 增加如下配置:
<profiles>  
<profile>  
    <id>downloadSources</id>  
    <properties>  
        <downloadSources>true</downloadSources>  
        <downloadJavadocs>true</downloadJavadocs>             
    </properties>  
</profile>  
</profiles>  
  
<activeProfiles>  
  <activeProfile>downloadSources</activeProfile>  
</activeProfiles>  

3:配置eclipse

Window > Preferences > Maven and checking the "Download Artifact Sources" and "Download Artifact JavaDoc" options
 
参考;
http://blog.csdn.net/topwqp/article/details/8902863
 
 

Maven编译报错 Unknown lifecycle phase "mvn" 解决办法

[ERROR] Unknown lifecycle phase "mvn". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]

 搜索到一个靠谱的回答:

如果是eclipse继承环境,去掉mvn

 
Maven常用命令

mvn -version/-v 显示版本信息

mvn clean 清空生成的文件

mvn compile 编译

mvn test 编译并测试

mvn package 生成target目录,编译、测试代码,生成测试报告,生成jar/war文件

mvn site 生成项目相关信息的网站

mvn clean compile 表示先运行清理之后运行编译,会将代码编译到target文件夹中

mvn clean package 运行清理和打包

mvn clean install 运行清理和安装,会将打好的包安装到本地仓库中,以便其他的项目可以调用

mvn clean deploy 运行清理和发布

  • mvn clean package依次执行了clean、resources、compile、testResources、testCompile、test、war(jar)(打包)7个阶段;
  • mvn clean install依次执行了clean、resources、compile、testResources、testCompile、test、war(jar)(打包)、install8个阶段;
  •  maven-war-plugin将工程打包成war,而maven-install-plugin会将打好的war包放入本地开发环境的maven版本库中。

 

https://blog.csdn.net/whq12789/article/details/106568531

Java运行maven生成的jar文件

 

nohup java -jar reply-1.0-SNAPSHOT.jar >/dev/null 2>&1 &

 

运行 maven生成jar,提示没有“没有主清单属性”

参考网上解决方案:

出现这个问题的原因是 pom 中没有添加主程序入口 ,

我们打开 jar 中的 /META_INF/ MANIFEST.MF缺少项目启动项,即没有Main-Class

 

解决办法:

在配置中添加如下配置

<project>根目录下添加:

<build>
        <finalName>${project.artifactId}-${project.version}</finalName> <!---修改编译出来的jar包名 -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version> <!--这个改成对应的,或者注释-->
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.letcode.xx</mainClass> <!-- 指定程序入口点所在类 -->
                        </manifest>
                    </archive>
                </configuration>

            </plugin>
        </plugins>

 

maven项目依赖,找不到依赖jar包的配置文件

 

在pom中加入maven-resources-plugin插件,和指定resources的位置。
1、在父类pom中加入maven-resources-plugin插件

<plugins>  
    <!-- 加载依赖模块的resources start -->  
    <plugin>  
        <groupId>org.apache.maven.plugins</groupId>  
        <artifactId>maven-resources-plugin</artifactId>  
        <version>2.5</version>  
        <configuration>  
            <encoding>UTF-8</encoding>  
        </configuration>  
    </plugin>  
    <!-- 加载依赖模块的resources end -->  
</plugins>  

2、在要打成war包的项目pom中加指定resources位置

<build>  
    <resources>  
        <resource>  
            <directory>src/main/resources</directory>  
            <filtering>true</filtering>  
        </resource>  
    </resources>  
</build> 



运行jar包出现
 

java -jar zookeeperLearn1-1.0-SNAPSHOT.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/zookeeper/Watcher
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)

 

看了下打出的jar包才13Kb,没有zookeeper的依赖库。

解决方法:让maven整理出依赖的jar包或者把依赖的jar包都打进去我们生成jar包里面去

  <build>
        <!--<finalName>${project.artifactId}</finalName>--><!--修改编译出来的jar包名,仅为{artifactId}.jar-->
        <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>

            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>highAvailability.DistributeServer</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>
                            <outputDirectory>
                                ${project.build.directory}/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


        </plugins>

    </build>

 


 

Maven构建可执行的jar包(包含依赖jar包)

maven构建jar包的步骤:

1.执行可执行的class,代码内需要有入口main方法

2.通过mvn package来构建jar包

3.使用java -jar test.jar来执行jar包

 

一、包含依赖jar包


 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>

    <groupId>com.xxx.delon</groupId>
    <artifactId>bugly</artifactId>
    <version>1.0-SNAPSHOT</version>

    
    <dependencies>
    <!-- 工程所需jar包引用开始 -->
        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>

        <!-- 代码所需依赖 -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.3.0</version>
        </dependency>

        <!-- 代码所需依赖 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.2.2</version>
        </dependency>
<!-- 工程所需jar包引用开始 --> </dependencies> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest>
<!-- 此处指定main方法入口的class --> <mainClass>com.xxx.uploadFile</mainClass> </manifest> </archive> ta </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
复制代码

生成jar包,会生成在target目录下

mvn package

解压缩bugly-1.0-SNAPSHOT.jar->META-INF->MANIFEST.MF

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: delon
Build-Jdk: 1.8.0_112
Main-Class: com.xxx.uploadFile

执行:

# 运行jar
java -jar test.jar

也可以通过如下命令

mvn assembly:assembly
#跳过测试 
mvn -Dmaven.test.skip=true  assembly:assembly

注意:在执行这个命令之前,必须先配置Maven的环境变量,检查是否配置可通过命令: mvn -version

如果上面的命令成功执行,那么在项目路径的target文件下就会有两个jar文件,一个是有jar包依赖的,一个是没jar包依赖的。

二、不包含依赖jar包


如果不想包含依赖的jar包,可以把<build>里面的代码替换成如下code:

复制代码
            <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-jar-plugin</artifactId>  
                <configuration>  
                    <archive>  
                        <manifest>  
                            <addClasspath>true</addClasspath>  
                            <classpathPrefix>lib/</classpathPrefix>  
                            <mainClass>com.xxx.uploadFile</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>  
                            <outputDirectory>  
                                ${project.build.directory}/lib  
                            </outputDirectory>  
                        </configuration>  
                    </execution>  
                </executions>  
            </plugin>  
复制代码

 

三、只包含部分依赖jar包


 如果想只包含部分依赖jar包

比如说,想做一个工具jar包,依赖公共jar和自己本地jar包,本地jar包需要解压成class打到jar包内,而依赖的公共jar包则不需要。

剔除公共jar包 可以用<scope>

<scope>的值的含义:
compile,缺省值,适用于所有阶段,会随着项目一起发布。
provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。

编译的时候采用 compile

复制代码
<dependency>
     <groupId>log4j</groupId>
     <artifactId>log4j</artifactId>
     <version>1.2.17</version>
     <scope>complie</scope>
     <optional>true</optional>
</dependency>
复制代码

在用package打包的时候,改成test,生成的jar包里就不会有该jar包的类了。

复制代码
<dependency>
     <groupId>log4j</groupId>
     <artifactId>log4j</artifactId>
     <version>1.2.17</version>
     <scope>test</scope>
     <optional>true</optional>
</dependency>
复制代码

build配置项,mainClass为空因为不是可执行jar。

复制代码
<build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass></mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
复制代码

 

参考链接:

IDEA maven 打可执行jar包 包括依赖jar文件和所有配置文件资源文件

使用nexus在局域网内搭建maven私服及idea的使用

maven 打包可执行jar的方法 :有三种方法,有点参考意义

 
 
参考;https://www.cnblogs.com/dzblog/p/6913809.html