Eclipse maven問題匯總


在使用eclipse+maven的過程中,遇到一系列問題,先匯總記錄如下:

1. 在java工程中,缺少Maven依賴:

image

這個問題比較棘手,一般都對eclispe的工程結構不是很了解,后來經過長時間的排查,發現在工程路徑下有一個.classpath文件,正常情況下,這個文件是這個樣子:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src/main/java"/>
    <classpathentry kind="src" path="src/test/java"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="owner.project.facets" value="maven.deps"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
    <classpathentry kind="lib" path="E:/CVS/FileIO/lib/sec.jar"/>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

 

如果在第一張圖中,沒有Maven dependencies,那么就是因為.classpath文件中 沒有配置org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER, 如果出現這中情況,可以在工程路徑下的.classpath文件中加入這幾行:

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="owner.project.facets" value="maven.deps"/>
        </attributes>
    </classpathentry>

然后重啟eclipse,就可以正常的看到maven dependencies了。這種解決方法針對標准版本的eclispe,比如Eclispe for Java EE Developer.

image

如果是商業版本的eclipse,比如MyEclipse 2015有更方便的解決方案:

image

 

image

在MyEclipse Project Facets中,選擇Maven Support-Dependencies Only,就可以將maven dependencies加入到工程中:

image

 

2. 第二個問題是外部安裝maven,並且選擇安裝源和m2路徑的設置:

下載好apache-maven:

image

解壓:

image

在我的電腦中設置,M2_HOME環境變量:

3e21951137434e8caf82e6543427b251

在path中添加Maven的bin:

%M2_HOME\bin;

在命令行中可以看到:

image

在eclipse中,設置剛才安裝的maven:

image

在maven的安裝路徑的conf目錄下有settings.xml:

image

<settings 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/settings-1.0.0.xsd">  
 <mirrors>
    <mirror>
      <id>CN</id>
      <name>OSChina Central</name>                                                                                                                       
      <url>http://maven.oschina.net/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>
  
  <localRepository>D:\Java\m2\repository</localRepository>
</settings>

然后在eclipse中設置,選擇剛才設置好的settings.xml文件,可以看到local repository中的路徑變成settings.xml中設置的本地路徑:

image

3. eclipse中soruce path和output dir出現問題,導致工程自己找不到自己的包

image

image

image

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM