简体   繁体   English

Java项目使用Maven编译,但是Eclipse仍然显示错误

[英]Java project compiles with Maven but Eclipse still shows errors

I am new to Maven and an using it to build my Java project. 我是Maven的新手,并且正在使用它来构建我的Java项目。 I have two questions: 我有两个问题:

  1. I was able to successfully compile my project with Maven but Eclipse still reports compile time errors. 我能够使用Maven成功编译我的项目,但是Eclipse仍然报告编译时错误。 I know these errors are because I have not added external jars to Eclipse's build path, but is there any other way I can resolve these errors? 我知道这些错误是因为我没有在Eclipse的构建路径中添加外部jar,但是还有其他方法可以解决这些错误吗?
  2. How do I run my Java project with Maven? 如何使用Maven运行Java项目?

Here's my pom.xml file: 这是我的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.springhibernate</groupId>
  <artifactId>SpringHibernateAssignment</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringHibernateAssignment</name>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
        <resource>
            <directory>src</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                    <configuration>
                        <groupId>com.springhibernate</groupId>
                        <artifactId>SpringHibernateAssignment</artifactId>
                        <version>1.0</version>
                        <packaging>jar</packaging>
                        <file>${basedir}/lib/ojdbc14.jar</file>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

    <pluginManagement>
        <plugins>
            <!--This plugin's configuration is used to store Eclipse m2e settings 
                only. It has no influence on the Maven build itself. -->
            <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-install-plugin
                                    </artifactId>
                                    <versionRange>
                                        [2.4,)
                                    </versionRange>
                                    <goals>
                                        <goal>install-file</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
  </build>
</project>

Put the third party libraries as dependencies in your pom.xml. 将第三方库作为依赖项放入pom.xml中。

<dependencies>
  <dependency>
    <groupId>javax.xml</groupId>
    <artifactId>jaxr-api</artifactId>
    <version>1.0</version>
  </dependency>
<dependencies>

You can find them in here . 您可以在这里找到它们。

The m2e plug in for Eclipse will integrate Maven so that Eclipse will automatically include JAR's from the local Maven cache based on the contents of the POM.xml. Eclipse的m2e插件将集成Maven,以便Eclipse将基于POM.xml的内容自动包括来自本地Maven缓存的JAR。

And excluding all Java source files explains why Maven's build succeeds without any dependencies. 并且排除所有Java源文件可以解释为什么Maven的构建在没有任何依赖的情况下能够成功进行。

If those jars you mention are public frameworks, you should be able to find them in the maven central repository and add them to your project just like Eranda explained. 如果您提到的这些jar是公共框架,那么您应该能够在maven中央存储库中找到它们并将其添加到您的项目中,就像Eranda解释的那样。 If you have jars that cannot be found publicly, you have to add them first to your local maven repo using maven install-file plugin: https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html 如果您有无法公开找到的jar,则必须先使用maven安装文件插件将其添加到本地Maven存储库中: https : //maven.apache.org/guides/mini/guide-3rd-party-jars- local.html

May be your M2_Home is not set, can you confirm. 可能是您的M2_Home未设置,可以确认。 You may be building the project from the command line not from the eclipse ( Guess). 您可能是从命令行而不是从Eclipse(Guess)构建项目。

Suggestions: 1. Set M2_HOME. 建议:1.设置M2_HOME。 2. Clean or Build the Project from eclipse, it will add all your dependent jars to references 2.从eclipse清理或构建项目,它将所有依赖的jar添加到引用中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM