How to tell Maven to include the jar dependency, not the subproject source directory in Eclipse?

后端 未结 6 1466
情歌与酒
情歌与酒 2021-02-13 11:11

I use Apache Maven to manage my Java libs (jar) and some of projects that use the libs. For convinience, I use mvn eclipse:eclipse to generate Eclipse project f

相关标签:
6条回答
  • 2021-02-13 11:38

    I have recently experienced that this behaviour is triggered by the version stated in the pom.xml files. If the version of the lib your primary project is depending on is available to eclipse in the workspace (e.g. a project with the matching pom.xml [group,artifact,version] for the lib) this will be used as a project reference in stead of being pulled from the M2REPO-location.

    My solution was to change the version of the library pom.xml in the workspace to another version allowing me to improve the lib, while the primary project still pulled the older lib from M2REPO.

    Using this by aligning the version numbers allows you to perform concurrent artifact improvements/alterations with little hassle.

    I have not yet tested if you need to run the mvn eclipse:eclipse every time you change the lib pom.xml files and the primary pom.xml dependencies, but I would expect it to be so.

    0 讨论(0)
  • 2021-02-13 11:44

    I believe this is actually because they're related projects in Eclipse. So if you right-click on your main project and go to Project References your lib project should be ticked.

    If you run mvn eclipse:eclipse it will automatically add project references for any sub-projects (modules). You can change this with the useProjectReferences property of the maven eclipse plugin.

    It defaults to true, but

    When set to false, the plugin will not create sub-projects and instead reference those sub-projects using the installed package in the local repository

    To use the property either set the property in your pom.xml

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <useProjectReferences>false</useProjectReferences>
                </configuration>
            </plugin>
        <plugins>
    <build>
    

    Or as a property on the command line:

    mvn eclipse:eclipse -Declipse.useProjectReferences=false
    
    0 讨论(0)
  • 2021-02-13 11:47

    Just try using the type element inside dependency tag and mention source/jar as the type in your pom.xml

    <!-- pom.xml for the main project --> 
    <dependency>
       <groupId>namespace.my</groupId>
       <artifactId>mylib</artifactId>
       <version>[1.0, )</version>
       <type>jar<type> <!-- source/jar -->
     </dependency> 
    
    0 讨论(0)
  • 2021-02-13 11:55

    I need to avoid to include the dependency as a refrenced project, but as a jar. Using Eclipse 4.6 Neon none of the previous solutions work.

    I had disable the Workspace Resolution feature

    click on the project -> Maven -> Disable Workspace Resolution
    
    0 讨论(0)
  • 2021-02-13 11:55

    Thanks for Hound Dog's hints. Yes, useProjectReferences controls Maven how to handle sub projects -- including source dir or including jar files.

    I haven't installed the maven plugin for Eclipse, so I add the following configuration in pom.xml. It tells Maven to include jar dependency in local maven repo, not the source dir. If the required jar file is not present in local repo, it will be automatically downloaded on the fly.

      <!-- pom.xml for the main project: include jar, not src path -->
      <plugin>
      <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.9</version>
        <configuration>
                <useProjectReferences>false</useProjectReferences>
        </configuration>
      </plugin>
    

    Now mvn eclipse:eclipse works as expected.

    0 讨论(0)
  • 2021-02-13 11:56

    I tried closing the project in eclipse & the workspace just built itself, this time taking the jar from the repository instead of linking the project

    0 讨论(0)
提交回复
热议问题