How to include dependency with 'provided' scope with maven-assembly-plugin

前端 未结 5 434
被撕碎了的回忆
被撕碎了的回忆 2021-02-01 18:27

I am fighting with maven to include a managed dependency with \'provided\' scope into tar file by using the maven-assembly-plugin.

I use super parent pom file as a base

相关标签:
5条回答
  • 2021-02-01 18:33

    This can be done using the Assembly plugin.

    First create an assembly.xml with the following:

    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
        <id>bin</id>
        <formats>
            <format>jar</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <dependencySets>
            <dependencySet>
                <unpack>true</unpack>
                <scope>runtime</scope>
            </dependencySet>
            <dependencySet>
                <unpack>true</unpack>
                <scope>provided</scope>
            </dependencySet>
        </dependencySets>
    </assembly>
    

    Then just enable it in your pom.xml

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    This will create a yourproject-bin.jar that will include all the compile and provided resources exploded so they can be referenced in a classpath.

    java -cp yourproject-bin.jar com.yourcompany.Main
    
    0 讨论(0)
  • 2021-02-01 18:34

    You can simply define the scope into your assembly descriptor.

    0 讨论(0)
  • 2021-02-01 18:42

    You can override a managed dependency by declaring it inside a <dependencyManagement> tag in the POM where you want it to be overridden.

    In your case, you should add the following to your child pom:

    <dependencyManagement>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencyManagement>
    

    Note that this overrides everything declared in the parent POM's dependency management, i.e. you cannot leave version or scope undeclared and expect it to be inherited.

    0 讨论(0)
  • 2021-02-01 18:46

    it is not possible to override the 'provided' scope in maven.

    To solve this issue, I declared a variable in the parent pom that will define the artifact scope. In order to override the scope the only thing that has to be done, is to set new value for the variable in the inherited pom.

    See example below:

    parent pom:

        <properties>
                <log4j.version>1.2.8</log4j.version>
                <log4j.scope>provided</log4j.scope>
        </properties>
    .
    .
    .
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                    <version>${log4j.version}</version>
                    <scope>${log4j.scope}</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    

    Now in the child pom, just declare the variable again:

    <properties>
            <log4j.scope>runtime</log4j.scope>
    </properties>
    
    0 讨论(0)
  • 2021-02-01 18:50

    I encountered a similar issue, trying to assemble a project with a dependency with scope "provided". I found a workaround for this issue:

    • Leave the dependency in "provided" scope
    • Copy the dependency to the target folder using the maven-dependency-plugin (example)
    • Use a fileSet in the assembly descriptor to bundle the dependency as a file.
    0 讨论(0)
提交回复
热议问题