【问题标题】:Using maven-assembly-plugin with local dependencies将 maven-assembly-plugin 与本地依赖项一起使用
【发布时间】:2014-12-16 09:42:45
【问题描述】:
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
                <manifest>
                    <mainClass>com.company.Main</mainClass>
                </manifest>
            </archive>
        </configuration>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

问题是我想在项目中保留一些本地依赖项。

我在pom.xml 中这样定义它们:

    <dependency>
        <groupId>groupid</groupId>
        <artifactId>local</artifactId>
        <version>1.1</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/local-utilities-1.1.jar</systemPath>
    </dependency>

现在,当我打包整个东西时,maven-assembly-plugin 只打包了自动下载并在 repo 中可用的依赖项...这可能是因为那些本地 JAR 在编译阶段可用,而不是在打包阶段可用(对吗?)。

如何让这个插件也包含这些依赖项?我尝试将 &lt;scope&gt; 更改为 package 和其他阶段,但显然 Maven 不允许这样做。

【问题讨论】:

标签: java maven plugins jar


【解决方案1】:

您需要“手动”复制这些依赖项,以便打包程序可以包含它们。

<build>
        <pluginManagement>      
            <plugins>
                <!-- Ignore/Execute plugin execution -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <!-- copy-dependency plugin -->
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-dependency-plugin</artifactId>
                                        <versionRange>[1.0.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-dependency-plugin</artifactId>
                    <version>2.4</version>
                <executions>
                           <execution>
                            <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>

                </executions>
                <configuration>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                </configuration>
            </plugin>

            <plugin>
                <!-- creates one single JAR, run: mvn assembly:single | mvn install -->
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                            </execution>
                </executions>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>${mainClass}</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>

        </plugins>

    </build>

【讨论】:

    猜你喜欢
    • 2013-02-19
    • 2014-03-12
    • 2011-12-31
    • 2011-02-05
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 2013-08-19
    相关资源
    最近更新 更多