java maven 将本地类目录添加到模块的类路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9925263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:55:27  来源:igfitidea点击:

maven add a local classes directory to module's classpath

javamavenclasspath

提问by Eldad Assis

I know this is a bit out of maven's scope, but I need to add a local directory with compiled classes to the module's classpath. I saw: Maven: add a folder or jar file into current classpath, but this is good for a jar only.

我知道这有点超出了 maven 的范围,但是我需要将一个包含编译类的本地目录添加到模块的类路径中。我看到:Maven: add a folder or jar file into current classpath,但这仅适用于 jar。

I need to have a similar solution but with compiled classes in a directory on local file system. Is this even possible?

我需要一个类似的解决方案,但在本地文件系统的目录中编译类。这甚至可能吗?

Thx!

谢谢!

采纳答案by Eldad Assis

After some extensive research, I found that my best option was to use the maven-antrun-plugin and during the process-resources phase, generate a jar from the classes and add it as dependency with system scope and systemPath to the jar I just built.

经过一些广泛的研究,我发现我最好的选择是使用 maven-antrun-plugin 并在 process-resources 阶段,从类生成一个 jar 并将其作为依赖项添加到我刚刚构建的 jar 中,具有系统范围和 systemPath .

Pom snippets:

Pom 片段:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
    <execution>
        <phase>process-resources</phase>
        <goals>
            <goal>run</goal>
        </goals>
        <configuration>
            <target name="mpower.jar">
                <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${env.USERPROFILE}\.m2\repository\ant-contrib\ant-contrib.0b3\ant-contrib-1.0b3.jar"/>

                <if>
                    <available file="${classes.dir}" type="dir"/>
                    <then>
                        <jar destfile="${env.TEMP}\classes.jar">
                            <fileset dir="${classes.dir}\classes">
                                <include name="**/**"/>
                            </fileset>
                        </jar>
                    </then>
                    <else>
                        <fail message="${classes.dir} not found. Skipping jar creation"/>
                    </else>
                </if>
            </target>
        </configuration>
    </execution>
</executions>

....

....

    <dependency>
        <groupId>ant-contrib</groupId>
        <artifactId>ant-contrib</artifactId>
        <version>1.0b3</version>
    </dependency>
    <dependency>
        <groupId>com.my.code</groupId>
        <artifactId>classes.jar</artifactId>
        <version>1.1</version>
        <scope>system</scope>
        <systemPath>${env.TEMP}\classes.jar</systemPath>
    </dependency>