【问题标题】:maven add a local classes directory to module's classpathmaven 将本地类目录添加到模块的类路径
【发布时间】:2012-04-13 02:14:09
【问题描述】:

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

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

谢谢!

【问题讨论】:

    标签: java maven classpath


    【解决方案1】:

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

    Pom sn-ps:

    <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\1.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>
    

    【讨论】:

    • 非常感谢您在此处记录此内容。我有 Javassist 动态生成的类,我无法在测试中使用它们。尝试了 Maven Build Helper 插件,但它不起作用,因为我没有代码,只有类文件。这是唯一真正让我前进的事情。谢谢!
    猜你喜欢
    • 2018-09-24
    • 1970-01-01
    • 2013-11-25
    • 2016-06-03
    • 2010-11-22
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2014-09-29
    相关资源
    最近更新 更多