下载java maven intelJ

环境搭建不停的百度

搞运维的不要只会部署发版,开发会的 运维也会 开发不会的运维还会才叫运维。。。写个java嘛 so easy 就是环境 依赖 配置比较头大,代码逻辑思想 搞懂了 写代码intelJ光补全呢

创建一个maven quickstart项目

mvn archetype:generate -DgroupId=com.opdays.learn -DartifactId=learn

一路回车

pom.xml

https://mvnrepository.com/ 在这里搜索自己用到的第三方包 我用了httpclient json解析配置到pom.xml dependencies段

然后project段加入build段 下面的意思是打包成jar的时候 顺便把依赖的jar包打包到lib目录



<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.feidee.linux</groupId>
<artifactId>linux</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>linux</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.34</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>

</dependencies>
<build>
<plugins>
<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>
</project>

目录结构

learn

   src

       main

        com.opdays.learn.App.java

        com.opdays.learn.Music

       test

   pom.xml 

随便写一点代码 代码里面用到httpclient的方法和json的方法就行了

mvn package

cd target

java -jar learn-1.0-SNAPSHOT.jar

learn-1.0-SNAPSHOT.jar中没有主清单属性

因为没有吧main方法写入MANIFEST.MF,那就要指定主方法在所在的类 就不能-jar了 需要使用-cp 指定classpath

java -cp learn-1.0-SNAPSHOT.jar com.opdays.learn.App

报错,找不到第三方依赖库

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest

        at java.lang.Class.getDeclaredMethods0(Native Method)

        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)

        at java.lang.Class.privateGetMethodRecursive(Unknown Source)

        at java.lang.Class.getMethod0(Unknown Source)

        at java.lang.Class.getMethod(Unknown Source)

        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)

        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest

        at java.net.URLClassLoader.findClass(Unknown Source)

        at java.lang.ClassLoader.loadClass(Unknown Source)

        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

        at java.lang.ClassLoader.loadClass(Unknown Source)

        ... 7 more

mvn package的时候把依赖复制到了target的lib目录 需要指定

java -cp learn-1.0-SNAPSHOT.jar -cp ./lib com.opdays.learn.App

错误: 找不到或无法加载主类 com.opdays.learn.App 看来-cp 不能指定两个 还是目录

如果需要指定的classpath是目录 指定-Djava.ext.dirs=

java -cp learn-1.0-SNAPSHOT.jar -Djava.ext.dirs=./lib  com.opdays.learn.App

可以运行了

如果依赖库分布在多个目录windows分号分割搜索目录下的jar文件

java  -Djava.ext.dirs=./lib;.  com.opdays.learn.App


java -D 参数指定java 启动程序的System类的属性 System.out.println(System.getProperties()); 可以获取全部

System.getProperty("java.ext.dirs") 获取单个-D的属性的值


如果需要将依赖打包到jar文件 加入下面的代码到pom plugins块

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.opdays.learn.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>

mvn clean package assembly:single

cd target

java -jar learn-1.0-SNAPSHOT-jar-with-dependencies.jar  可以直接运行

百度真是不行 一大堆错的没用的,非要老子谷歌https://stackoverflow.com/questions/16514764/how-to-make-a-runnable-jar-of-my-maven-project

mvn assembly:help

冒号前是插件 冒号后是goal

Maven Assembly Plugin
  A Maven 2 plugin to create archives of your project's sources, classes,
  dependencies etc. from flexible assembly descriptors.
This plugin has 8 goals:
assembly:assembly
  Assemble an application bundle or distribution using an assembly descriptor
  from the command line. This goal will force Maven to build all included POMs
  up to the package phase BEFORE the assembly is processed.
  NOTE: This goal should ONLY be run from the command line, and if building a
  multimodule project it should be used from the root POM. Use the
  assembly:single goal for binding your assembly to the lifecycle.
assembly:attached
  Deprecated. Use goal: 'assembly' (from the command line) or 'single' (from a
  lifecycle binding) instead.
assembly:directory
  Like the assembly:attached goal, assemble an application bundle or
  distribution using an assembly descriptor from the command line. This goal
  will force Maven to build all included POMs up to the package phase BEFORE the
  assembly is processed. This goal differs from assembly:assembly in that it
  ignores the <formats/> section of the assembly descriptor, and forces the
  assembly to be created as a directory in the project's build-output directory
  (usually ./target).
  This goal is also functionally equivalent to using the assembly:assembly goal
  in conjunction with the dir assembly format.
  NOTE: This goal should ONLY be run from the command line, and if building a
  multimodule project it should be used from the root POM. Use the
  assembly:directory-single goal for binding your assembly to the lifecycle.
assembly:directory-inline
  Deprecated. Use goal: 'directory' (from the command line) or
  'directory-single' (from a lifecycle binding) instead.
assembly:directory-single
  Like the assembly:attached goal, assemble an application bundle or
  distribution from an assembly descriptor. This goal is suitable either for
  binding to the lifecycle or calling directly from the command line (provided
  all required files are available before the build starts, or are produced by
  another goal specified before this one on the command line).
  This goal differs from assembly:single in that it ignores the <formats/>
  section of the assembly descriptor, and forces the assembly to be created as a
  directory in the project's build-output directory (usually ./target).
assembly:help
  Display help information on maven-assembly-plugin.
  Call
    mvn assembly:help -Ddetail=true -Dgoal=<goal-name>
  to display parameter details.
assembly:single
  Assemble an application bundle or distribution from an assembly descriptor.
  This goal is suitable either for binding to the lifecycle or calling directly
  from the command line (provided all required files are available before the
  build starts, or are produced by another goal specified before this one on the
  command line).
assembly:unpack
  Deprecated. Use org.apache.maven.plugins:maven-dependency-plugin goal: unpack
  or unpack-dependencies instead.