1. 程式人生 > >mvn package方法打包注入依賴解決方法

mvn package方法打包注入依賴解決方法

方法一:將jar包和專案打在一起

1.修改pom根節點下packaging屬性為jar
2.修改pom根節點下build為:

<build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef
>
jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.hbase2Redis.Hbase2Redis</mainClass> //這個是打包程式的主程式類 </manifest
>
</archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals
>
<goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

3.使用mvn install 打包,將在target下生成專案jar包xxx-with-dependencies.jar複製出來,執行java -jar xxx-with-dependencies.jar即可執行

方法二:將jar包和專案分離

1.修改pom根節點下packaging屬性為jar
2.修改pom根節點下build為:

<build>
        <finalName>Hbase2Redis</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-dependency-plugin</artifactId>
                                        <versionRange>[2.0,)</versionRange>
                                        <goals>
                                            <goal>copy-dependencies</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <!-- 主方法所在類名 -->
                            <mainClass>com.hbase2Redis.Hbase2Redis</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!-- 將依賴包放到lib資料夾中 -->
            <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>

3.使用mvn install 打包,將在target下生成專案jar包xxx.jar及lib資料夾,將這兩者放在同一目錄下,執行java -jar xxx.jar即可執行。