南去北来徒自老,故人稀。

Maven

关于使用Idea中maven工具打包没有主类的问题

(1)没有配置阿里云仓库

如果没有配置相应的阿里云仓库则可能无法下载相关的依赖。我出现的问题就是无论如何找不到assembly的plugin,而要使jar 包含有主类的必要条件就是需要用到这个plugin。 所以首先需要在依赖中添加以下代码,

  <repositories>
      <repository>
      <id>nexus-aliyun</id>
      <name>nexus-aliyun</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

(2)没有利用maven-assembly-plugin这个插件添加主类!

添加以下代码!!!可以让你的主类包含在jar包中。

	<plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>3.0.0</version>
          <configuration>
            <archive>
              <manifest>
                <mainClass>org.example.CalenderTest</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>

(3)生成了含有“with-dependencies.jar”这个后缀的jar包!

可以在configuration标签下添加如下内容

<appendAssemblyId>false</appendAssemblyId>

(4)如果以上均没有问题可以试试再 pluginManagement标签下添加一个plugins与其同级的标签!

一定要在pluginManagement外面添加

    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.0.0</version>
      </plugin>
    </plugins>

springboot maven 多模块打包

问题:多模块项目,打包jar,只有几kb,java -jar xx.jar 报错找不到主类

然后开始了爬坑之旅,解决总结如下:

1.父pom 不要有 spring-boot-maven-plugin 插件!!!可以加一个 maven-surefire-plugin 插件 跳过test类 防止编译报错。

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2.任何被主模块依赖的子模块,不能有 spring-boot-maven-plugin 插件 !!! 定义模块时候添加 jar

    <groupId>cn.javacv</groupId>
    <version>1.0-SNAPSHOT</version>
    <artifactId>chenyu-common</artifactId>
    <packaging>jar</packaging>

3.只有主模块才能添加 spring-boot-maven-plugin 插件

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

img

参考博客:https://blog.csdn.net/qq_29765371/article/details/99234759

Maven中的依赖作用范围概述

Maven中使用 scope 来指定当前包的依赖范围和依赖的传递性。常见的可选值有:compile, provided, runtime, test, system 等。scope 主要是用在 pom.xml 文件中的依赖定义部分,例如:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.1.RELEASE</version>
            <scope>test</scope>
        </dependency>

scope各种取值详解

scope取值 有效范围(compile, runtime, test) 依赖传递 例子
compile all spring-core
provided compile, test servlet-api
runtime runtime, test JDBC驱动
test test JUnit
system compile, test  

正如上表所示,

compile :为默认的依赖有效范围。如果在定义依赖关系的时候,没有明确指定依赖有效范围的话,则默认采用该依赖有效范围。

此种依赖,在编译、运行、测试时均有效。

provided :在编译、测试时有效,但是在运行时无效。

provided意味着打包的时候可以不用包进去,别的设施(Web Container)会提供。

事实上该依赖理论上可以参与编译,测试,运行等周期。相当于compile,但是在打包阶段做了exclude的动作。

例如:servlet-api,运行项目时,容器已经提供,就不需要Maven重复地引入一遍了。

runtime :在运行、测试时有效,但是在编译代码时无效。

说实话在终端的项目(非开源,企业内部系统)中,和compile区别不是很大。比较常见的如JSR×××的实现,对应的API jar是compile的,具体实现是runtime的,compile只需要知道接口就足够了。

例如:JDBC驱动实现,项目代码编译只需要JDK提供的JDBC接口,只有在测试或运行项目时才需要实现上述接口的具体JDBC驱动。

另外runntime的依赖通常和optional搭配使用,optional为true。我可以用A实现,也可以用B实现。

test :只在测试时有效,包括测试代码的编译,执行。例如:JUnit。

PS: test表示只能在src下的test文件夹下面才可以使用,你如果在a项目中引入了这个依赖,在b项目引入了a项目作为依赖,在b项目中这个注解不会生效,因为scope为test时无法传递依赖。

system :在编译、测试时有效,但是在运行时无效

和provided的区别是,使用system范围的依赖时必须通过systemPath元素显式地指定依赖文件的路径。由于此类依赖不是通过Maven仓库解析的,而且往往与本机系统绑定,可能造成构建的不可移植,因此应该谨慎使用。

systemPath元素可以引用环境变量。例如:

          <dependency>
            <groupId>javax.sql</groupId>
            <artifactId>jdbc-stdext</artifactId>
            <version>2.0</version>
            <scope>system</scope>
            <systemPath>${java.home}/lib/rt.jar</systemPath>
        </dependency>

scope的依赖传递

A–>B–>C。当前项目为A,A依赖于B,B依赖于C。知道B在A项目中的scope,那么怎么知道C在A中的scope呢?

答案是:

当C是test或者provided时,C直接被丢弃,A不依赖C; 否则A依赖C,C的scope继承于B的scope。

版权声明:如无特别声明,本站收集的文章归  HuaJi66/Others  所有。 如有侵权,请联系删除。

联系邮箱: GenshinTimeStamp@outlook.com

本文标题:《 Maven Zero 》

本文链接:/%E5%BC%80%E5%8F%91%E7%9B%B8%E5%85%B3/%E6%9E%84%E5%BB%BA%E5%B7%A5%E5%85%B7/maven-zero.html