简体   繁体   English

向我的maven项目添加非jar依赖项

[英]Adding a non-jar dependency to my maven project

I'd like to add an XML file to my project's dependencies. 我想在项目的依赖项中添加一个XML文件。 I've been pretty successful in doing this but unfortunately I've found that my XML file is not added to the classpath (which kind of makes sense as it is not a jar file). 我已经非常成功地做到了这一点,但遗憾的是我发现我的XML文件没有被添加到类路径中(哪种有意义,因为它不是一个jar文件)。

The reason I want to have the XML file in my classpath is so that I could load it as a resource. 我想在我的类路径中使用XML文件的原因是我可以将其作为资源加载。

What is the recommended way of doing this, if it is at all recommended? 如果完全建议,建议的方法是什么?

Thanks 谢谢

This solution works for me. 这个解决方案适合我。 I am using a WMQ table channel file from a Nexus repository. 我正在使用来自Nexus存储库的WMQ表通道文件。

        <dependency>
            <groupId>wmq</groupId>
            <artifactId>wmq-channel-tab-file</artifactId>
            <version>1.0.0</version>
            <type>tab</type>
        </dependency>

... 

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>wmq</groupId>
                                    <artifactId>wmq-channel-tab-file</artifactId>
                                    <version>1.0.0</version>
                                    <type>tab</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/classes</outputDirectory>
                                    <destFileName>wmq-channel-tab-file.tab</destFileName>
                                </artifactItem>
                            </artifactItems>
                            <!-- other configurations here -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Yes, put it in the resource folder. 是的,把它放在资源文件夹中。 By default that's src\\main\\resources . 默认情况下是src\\main\\resources If you put a file in there, it'll become available in your classpath. 如果你把文件放在那里,它将在你的类路径中可用。

Alternatively, you could modify your pom to say your folder where the xml file is in is a resource folder. 或者,您可以修改您的pom以说明xml文件所在的文件夹是资源文件夹。 But, I consider this a bad practice if your xml file is under your src\\main\\java directory. 但是, 如果您的xml文件位于src\\main\\java目录下,我认为这是一种不好的做法。

You need to put the file in src/main/resources. 您需要将文件放在src / main / resources中。 After that the file is available on the classpath. 之后,该文件在类路径上可用。 Then it can be read with: 然后可以阅读:

    // Foo.class
    // assuming xml file is foo.xml
    InputStream is = Foo.class.getResourceAsStream("foo.xml"); 

Maven uses the resources folder for your project as you mention them in pom.xml. 当你在pom.xml中提到它时,Maven使用你的项目的resources文件夹。 Here is a sample of pom. 这是一个pom的样本。 Anything added as a resource will be available in your classpath and can be accessed in your code. 作为资源添加的任何内容都将在您的类路径中提供,并且可以在您的代码中访问。

  <build>
    <finalName>Examples</finalName>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <directory>src/main/config</directory>
        </resource>
    </resources>
</build>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM