Maven 2 assembly with dependencies: jar under scope “system” not included

随声附和 提交于 2019-11-26 03:38:27

I'm not surprised that system scope dependencies are not added (after all, dependencies with a system scope must be explicitly provided by definition). Actually, if you really don't want to put that dependency in your local repository (for example because you want to distribute it as part of your project), this is what I would do:

  • I would put the dependency in a "file system repository" local to the project.
  • I would declare that repository in my pom.xml like this:

    <repositories>
      <repository>
        <id>my</id>
        <url>file://${basedir}/my-repo</url>
      </repository>
    </repositories>
    
  • I would just declare the artifact without the system scope, this is just a source of troubles:

    <dependency>
      <groupId>sourceforge.jchart2d</groupId>
      <artifactId>jchart2d</artifactId>
      <version>3.1.0</version>
    </dependency>
    

I'm not 100% sure this will suit your needs but I think it's a better solution than using the system scope.

Update: I should have mentioned that in my original answer and I'm fixing it now. To install a third party library in the file-based repository, use install:install-file with the localRepositoryPath parameter:

mvn install:install-file -Dfile=<path-to-file> \
                         -DgroupId=<myGroup> \
                         -DartifactId=<myArtifactId> \
                         -Dversion=<myVersion> \
                         -Dpackaging=<myPackaging> \
                         -DlocalRepositoryPath=<path-to-my-repo>

You can paste this as is in a *nix shell. On windows, remove the "\" and put everything on a single line.

Btw you can automate it and make it a part of your maven build. The following will install your jar into your local repository before compilation:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <executions>
                <execution>
                    <id>hack-binary</id>
                    <phase>validate</phase>
                    <configuration>
                        <file>${basedir}/lib/your-lib.jar</file>
                        <repositoryLayout>default</repositoryLayout>
                        <groupId>your-group</groupId>
                        <artifactId>your-artifact</artifactId>
                        <version>0.1</version>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                    </configuration>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

I find easy solution in case you creating jar

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.1.1</version>
  <configuration>
    <webResources>
    <resource>
      <directory>dependencies/mydep</directory>
        <targetPath>WEB-INF/lib</targetPath>
        <filtering>true</filtering>
        <includes>
           <include>**/*.jar</include>
        </includes>
    </resource>
  </webResources>
</configuration>
</plugin>
khmarbaise

You can also handle this via adding a supplemental dependencySet in your dependencySets.

<dependencySet>
  <scope>system</scope>
  <includes>
    <include>*:jar</include>
  </includes>
  <outputDirectory>lib</outputDirectory>
</dependencySet>

The best thing would be to use a Repository Manager (like Nexus, Artifactory, Archiva) and install this kind of dependency in a particular repository. After that you can use such things as a simple dependency. This will simplify your life.

Docs: https://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

Edited: Sorry that i didn't realize alx also mentioned about the clean life cycle workaround.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <executions>
        <execution>
            <id>hack-binary</id>
            <phase>clean</phase>
            <configuration>
                <file>${basedir}/lib/your-lib.jar</file>
                <repositoryLayout>default</repositoryLayout>
                <groupId>your-group</groupId>
                <artifactId>your-artifact</artifactId>
                <version>0.1</version>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
            </configuration>
            <goals>
                <goal>install-file</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Base on the solution provided by alx, you can execute the install file step at clean phase. but since the clean phase is not in the default life cycle, you have to execute mvn clean at the first time to ensure the jar is ready in the local repo.

ex: mvn clean; mvn package

A simple solution for this is to add it into local maven repository

One way to do is via mvn install commands as suggested in previous post .

Another easy way is , 1) In your eclipse ide right click on project select Maven option . 2) Select Install or deploy an artifact to a maven repository option and click on next. 3)Click on browse next to the Artifact file checkbox & select your jar file 4)Enter the GroupId and ArtifactId and version ensure generate pom & create checksum are checked & packaging is jar

Click on finish ,Wallah !!! your job is done the jar is added in your local repository which you can define in setting.xml or m2 directory

Now just add the simple maven dependency as per the GroupId,ArtifactId & jar version that you have entered as per the import and that's it your external jar will be packaged by maven.

it has worked in a easier way on my solution :

remove from your dependency :

<dependency>
  <groupId>tiago.medici</groupId>
  <artifactId>eureka</artifactId>
  <version>0.0.1</version>
</dependency> 

Then add the maven-install-plugin in the pom.xml as well.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-install-plugin</artifactId>
  <executions>
    <execution>
      <id>install-external</id>
      <phase>clean</phase>
      <configuration>
        <file>${basedir}/external/tiago.medici-0.0.1.jar</file>
        <repositoryLayout>default</repositoryLayout>
        <groupId>tiago.medici</groupId>
        <artifactId>eureka</artifactId>
        <version>0.0.1</version>
        <packaging>jar</packaging>
        <generatePom>true</generatePom>
      </configuration>
      <goals>
        <goal>install-file</goal>
      </goals>
    </execution>
  </executions>
</plugin>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!