maven 打包的几种方式

  • 打jar包独立依赖
<plugins>
            <!--      配置打包,并配置MainClass, 但不将依赖包打包,会出现找不到依赖的异常    -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!--   MANIFEST.MF 中 Class-Path -->
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>indi.cyh.EurekaServer</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!--      配置依赖包      -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!--              将依赖包打包至target下的lib目录              -->
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  • 打jar包所有依赖都在jar包(spring框架下有问题)
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>indi.cyh.EurekaServer</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>

  • 打jar包所有依赖都在jar包(spring)
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
posted @ 2022-02-26 14:09  故木  阅读(369)  评论(0编辑  收藏  举报