maven 常用配置

maven 常用配置:

关于pom文件的配置问题,为了省得每次去网上找,把一些常用的配置记下来。

Maven 构建,本地Jar包的依懒问题

一般Maven会从官方仓库或公司仓库下载依懒包。如果是无法下载的本地jar包。可以用以下两种方式来配置

方法1:<scope>system</scope>显式提供包含依赖的jar,Maven不会在Repository中查找它。

<dependency>

<groupId>org.apache</groupId>

<artifactId>XXX</artifactId>

<version>1.0</version>

<scope>system</scope>

<systemPath>${basedir}libXXX.jar</systemPath>

</dependency>

方法2:maven-compiler-plugin 配置exdirs。

<plugin>

<artifactId>maven-compiler-plugin</artifactId>

  <configuration>

    <encoding>UTF-8</encoding>

    <compilerArguments>

      <extdirs>${basedir}lib</extdirs>

    </compilerArguments>

  </configuration>

</plugin>

Maven 构建可运行的Jar 包

为了构建可运行的jar包,需要把依懒包一起打包。下面整理出了两种方式。

方法1:把依懒和项目代码放在一起打包。

     <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.rihai.sparktest2.WordCount</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

方法2:分两步走。第一步,把所有依懒放在一个文件夹里。第二步,项目打包,指定main函数, 指定引用的classpath 为前面的依懒包目录。

            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.rihai.sparktest2.WordCount</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <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>
原文地址:https://www.cnblogs.com/zrhai/p/5828947.html