鉴于以下 pom.xml:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myApp</groupId>
<artifactId>malloc</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<repositories>
<repository>
<id>project</id>
<url>file:///${basedir}/lib</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.myApp</groupId>
<artifactId>myApp.core</artifactId>
<version>1.0.0</version>
</dependency>
... //other dependencies
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<webResources>
<webResource>
<directory>${project.build.directory}/WebContent/WEB-INF</directory>
<includes>
<include>web.xml</include>
</includes>
<targetPath>WEB-INF</targetPath>
<filtering>true</filtering>
</webResource>
</webResources>
</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>
<outputDirectory>${project.build.directory}/WebContent/WEB-INF/lib</outputDirectory>
<includeScope>runtime</includeScope>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
以及以下项目结构:
我希望 maven-dependency-plugin 会将所有依赖项复制到 WebContent/WEB-INF/lib 但当我运行时
mvn clean install
生成以下错误:
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building api-malloc 0.0.1
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for com.oracle:ojdbc6:jar:10.2.0.4.0 is missing, no dependency information available
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ api-malloc ---
[INFO] Deleting C:\Development\malloc\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ api-malloc ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ api-malloc ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 8 source files to C:\Development\malloc\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Development/malloc/src/com/myApp/api/malloc/dto/RegionTypeDTO.java:[3,34] package com.myApp.api.core.dto does not exist
[ERROR] /C:/Development/malloc/src/com/myApp/api/malloc/dto/RegionTypeDTO.java:[5,39] cannot find symbol
symbol: class IDTO
[INFO] 26 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.209 s
[INFO] Finished at: 2015-11-26T15:33:40-05:00
[INFO] Final Memory: 21M/227M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project malloc: Compilation failure: Compilation failure:
[ERROR] /C:/Development/malloc/src/com/myApp/api/malloc/dto/RegionTypeDTO.java:[3,34] package com.myApp.api.core.dto does not exist
[ERROR] /C:/Development/malloc/src/com/myApp/api/malloc/dto/RegionTypeDTO.java:[5,39] cannot find symbol
[ERROR] symbol: class IDTO
[ERROR] location: class com.myApp.api.malloc.controllers.TestController
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
我注意到 WEB-INF/lib 文件夹总是空的,maven 依赖项没有被复制。
我怀疑复制依赖项插件在编译阶段之前没有运行,但我无法真正弄清楚 - 我错过了什么?
最佳答案
如果编译失败,copy-dependency
目标确实永远不会被执行,因为它绑定(bind)到 package
阶段,该阶段发生在 compile
阶段(默认情况下链接到 Maven 编译器插件)。
如果您希望它在 compile
阶段之前运行,则需要更改 copy-dependencies
目标执行的阶段值。更改为 process-resources
应该没问题,也应该有意义。
<phase>package</phase>
有关阶段的完整描述,您可以查看官方文档,here .
您还应该修复构建输出指向的编译错误。我看到 sourceDirectory
元素覆盖了 Maven 默认使用的 Java 源代码 (src\main\java
),因此我认为您的代码直接位于 src
文件夹。
更新:当代码引用 Java 编译器未解析的包时,会发生包 X 不存在
错误,因此无法看到类路径中的包,这意味着在声明的依赖项中:myApp.core
是否包含该包和类?如果是,则存储库元素(lib 文件夹)可能未正确提供 myApp 依赖项。
您可以尝试使用指定的 Maven 安装插件在 .m2
maven 缓存中本地安装依赖项 here 。
您可以从命令行执行如下:
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file
-Dfile=lib\myApp.core-1.0.0.jar \
-DgroupId=com.myApp \
-DartifactId=myApp.core \
-Dversion=1.0.0 \
-Dpackaging=jar \
<小时/>
旁注:Maven 更喜欢小写的 groupid 和 artifactid 标记。此外,Maven 约定也使用破折号来分隔标记(即 myapp-core
),而不是使用驼峰命名法(即 myApp
)。
关于java - 无法执行目标 org.apache.maven.plugins :maven-compiler-plugin:3. 1:compile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33946896/