maven copy dependecies不工作

时间:2015-10-21 13:58:47

标签: java maven jar dependencies classpath

这个问题已被问到,但没有一个解决方案适合我:

我想复制通过POM.xml构建中的依赖关系标签指定的jar:

<dependency>
...
</dependency>

进入target / lib

这样的文件夹

的pom.xml:

<build>
    <defaultGoal>install</defaultGoal>
    <directory>${basedir}/target</directory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.googlecode.addjars-maven-plugin</groupId>
                <artifactId>addjars-maven-plugin</artifactId>
                <version>1.0.5</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>add-jars</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>${project.build.directory}/my-repo</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <classpathPrefix>lib/</classpathPrefix>
                            <addClasspath>true</addClasspath>
                            <mainClass>Swapper</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeScope>runtime</includeScope>
                            <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
像我说的那样,根本不会复制任何内容。但根据该门户网站上的一些问答,它应该有效。

有什么想法吗?

3 个答案:

答案 0 :(得分:7)

您要在<pluginManagement>部分声明自己的插件。这一切都很好,但如果希望他们执行,您需要在<pluginManagement>部分之外声明它们:

<build>
  <pluginManagement>
     ...
  </pluginManagement>
  <plugins>
    <plugin>
     <artifactId>addjars-maven-plugin</artifactId>
    </plugin>
    <plugin>
     <artifactId>maven-jar-plugin</artifactId>
    </plugin>
    <plugin>
     <artifactId>maven-dependency-plugin</artifactId>
    </plugin>
  </plugins>
</build>

以与<pluginManagement>相同的方式考虑<dependencyManagement>,但是对于插件。

答案 1 :(得分:0)

尝试替换:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <classpathPrefix>lib/</classpathPrefix>
                <addClasspath>true</addClasspath>
                <mainClass>Swapper</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <includeScope>runtime</includeScope>
                <outputDirectory>${project.build.directory}/lib/</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

由此:

<build>
    <plugins> 
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId> 
            <configuration>
             <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                <archive>
                    <manifest>
                        <mainClass>Swapper</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs> 
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build> 

答案 2 :(得分:0)

根据您的插件添加如下所示的目标

clean install dependency:copy-dependencies