maven打包源码及jar到私有仓库、依赖本地jar

<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.0.0</version>
                    <configuration>
                        <attach>true</attach>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>jar-no-fork</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

maven"内置插件"中没有打包源代码的插件,所以,需要开发者配置插件。apache提供了maven-source-plugin插件用于打包源代码。

maven-source-plugin

The Source Plugin has five goals:

  • source:aggregate aggregrates sources for all modules in an aggregator project.
  • source:jar is used to bundle the main sources of the project into a jar archive.
  • source:test-jar on the other hand, is used to bundle the test sources of the project into a jar archive.
  • source:jar-no-fork is similar to jar but does not fork the build lifecycle.
  • source:test-jar-no-fork is similar to test-jar but does not fork the build lifecycle

在《maven实战》一书中,配置使用的是jar-no-fork

jar-no-fork与jar的区别

jar,在执行goal之前,执行generate-sources阶段,也就是说,如果,jar绑定的目标在generate-sources之后(比如verify)的话,generate-sources会执行两遍。

  • The goal is thread-safe and supports parallel builds.
  • Binds by default to the lifecycle phase: package
  • Invokes the execution of the lifecycle phase generate-sources prior to executing itself.

jar-no-fork,没有其余动作,在绑定的phase执行。原文是,

  • The goal is thread-safe and supports parallel builds.

<plugins>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1.1</version>
                <!-- 配置插件参数 -->
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

<!-- 发布组件 -->

<distributionManagement>

<repository>

<id>releases</id>

<url>http://0.9:8082/nexus/content/repositories/releases</url>

</repository>

<snapshotRepository>

<id>snapshots</id>

<url>http://.0.9:8082/nexus/content/repositories/snapshots</url>

</snapshotRepository>

</distributionManagement>

打包成jar供项目使用

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>

将本地jar打到springboot jar中

<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>
			<resource>
				<directory>${basedir}/src/main/webapp</directory>
				<targetPath>META-INF/resources</targetPath>
				<includes>
					<include>**/**</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>false</filtering>
				<excludes>
					<exclude>.gitkeep</exclude>
				</excludes>
				<includes>
					<include>**</include>
				</includes>
			</resource>
			<resource>
				<directory>${project.basedir}/lib</directory>
				<targetPath>BOOT-INF/lib/</targetPath>
				<includes>
					<include>**/*.jar</include>
				</includes>
			</resource>
		</resources>

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <includeSystemScope>true</includeSystemScope>
        </configuration>
    </plugin>
</plugins>

打包成springboot jar包,直接启动,不给其他项目提供依赖

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

依赖本地jar

本地工程打包

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

            <!-- -->
            <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.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>-->
                            <!--<outputDirectory>-->
                                <!--${project.build.directory}/target/classes/META-INF/lib-->
                            <!--</outputDirectory>-->
                        <!--</configuration>-->
                    <!--</execution>-->
                <!--</executions>-->
            <!--</plugin>-->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/generated-sources/java</outputDirectory>
                            <processor>org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

依赖本地jar

<dependency>
            <groupId>cn.fileserver</groupId>
            <artifactId>er</artifactId>
            <version>${fileserver.version}}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-data-mongodb</artifactId>
                </exclusion>
            </exclusions>
            <scope>system</scope>
            <systemPath>E:/1orkspace/target/e.jar</systemPath>
        </dependency>

构建项目的配置
项目构建,进行打包,使用<build></build>标签进行配置

构建时,可以使用不同的插件,指定不同的功能,常用的功能由如下三种:

maven-assembly-plugin
设置打包信息,生成的jar包的名称是a r t i f a c t I d − {artifactId}-artifactId−{version}-jar-with-dependencies.jar

maven-compiler-plugin
设置java的版本号

maven-resources-plugin
设置项目的编码格式

具体配置

    <build>
        <plugins>
            <!--插件1-->
            <!--打包配置的插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <!--官方提供的定制化打包方式,将所有依赖的jar包都添加到最终打的包里-->
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <!--名字-->
                        <id>make-my-jar-with-dependencies</id>
                        <!--绑定到package生命周期阶段上-->
                        <phase>package</phase>
                        <!--只执行一次-->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!--插件2-->
            <!--设置java版本为1.8-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <!--插件3-->
            <!--设置编码为utf-8-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

https://blog.youkuaiyun.com/xb12369/article/details/79966633

https://blog.youkuaiyun.com/q258523454/article/details/81945464

坑,使用以上文章的配置,内存设置到4g,仍然内存溢出;

使用如下方式解决:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值