簡體   English   中英

Java項目使用Maven編譯,但是Eclipse仍然顯示錯誤

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

我是Maven的新手,並且正在使用它來構建我的Java項目。 我有兩個問題:

  1. 我能夠使用Maven成功編譯我的項目,但是Eclipse仍然報告編譯時錯誤。 我知道這些錯誤是因為我沒有在Eclipse的構建路徑中添加外部jar,但是還有其他方法可以解決這些錯誤嗎?
  2. 如何使用Maven運行Java項目?

這是我的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>

將第三方庫作為依賴項放入pom.xml中。

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

您可以在這里找到它們。

Eclipse的m2e插件將集成Maven,以便Eclipse將基於POM.xml的內容自動包括來自本地Maven緩存的JAR。

並且排除所有Java源文件可以解釋為什么Maven的構建在沒有任何依賴的情況下能夠成功進行。

如果您提到的這些jar是公共框架,那么您應該能夠在maven中央存儲庫中找到它們並將其添加到您的項目中,就像Eranda解釋的那樣。 如果您有無法公開找到的jar,則必須先使用maven安裝文件插件將其添加到本地Maven存儲庫中: https : //maven.apache.org/guides/mini/guide-3rd-party-jars- local.html

可能是您的M2_Home未設置,可以確認。 您可能是從命令行而不是從Eclipse(Guess)構建項目。

建議:1.設置M2_HOME。 2.從eclipse清理或構建項目,它將所有依賴的jar添加到引用中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM