我如何让我的项目的运行时依赖复制到目标/lib文件夹?

就像现在一样,在mvn清洁安装后,目标文件夹只包含我的项目的jar,但没有运行时依赖项。


当前回答

对于需要将依赖项复制到目标目录而不使用maven的任何其他阶段的情况,一个简单而优雅的解决方案(我发现这在使用Vaadin时非常有用)。

完整的pom示例:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>groupId</groupId>
    <artifactId>artifactId</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>process-sources</phase>

                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>

                            <configuration>
                                <outputDirectory>${targetdirectory}</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
            </plugin>
        </plugins>
    </build>
</project>

然后执行mvn process-sources

jar文件依赖项可以在/target/dependency中找到

其他回答

最好的方法取决于你想做什么:

If you want to bundle your dependencies into a WAR or EAR file, then simply set the packaging type of your project to EAR or WAR. Maven will bundle the dependencies into the right location. If you want to create a JAR file that includes your code along with all your dependencies, then use the assembly plugin with the jar-with-dependencies descriptor. Maven will generate a complete JAR file with all your classes plus the classes from any dependencies. If you want to simply pull your dependencies into the target directory interactively, then use the dependency plugin to copy your files in. If you want to pull in the dependencies for some other type of processing, then you will probably need to generate your own plugin. There are APIs to get the list of dependencies, and their location on disk. You will have to take it from there...

如果您在Eclipse中的Tomcat服务器上运行时遇到与WEB-INF/lib文件中未出现依赖项相关的问题,请查看以下内容:

启动Tomcat时的DispatcherServlet (Maven依赖没有复制到wtpwebapps)

您只需在项目属性>部署程序集中添加Maven依赖项。

如果你的项目是war或ear类型,专家会复制依赖项。

mvn install dependency:copy-dependencies 

工作为我在目标文件夹中创建的依赖目录。喜欢它!

如果你想偶尔这样做(因此不想改变你的POM),试试这个命令行:

mvn dependency:copy-dependencies -DoutputDirectory=${project.build.directory}/lib

如果省略最后一个参数,依赖项将放在target/dependencies中。