如何编写maven build来为classpath添加资源?

时间:2022-02-04 19:55:32

I am building a jar using maven with simple maven install. If I add a file to src/main/resources it can be found on the classpath but it has a config folder where I want that file to go but moving it inside the config folder makes it disappear from the classpath.

我正在使用带有简单maven安装的maven构建一个jar。如果我将一个文件添加到src / main / resources,它可以在类路径中找到,但是它有一个config文件夹,我希望该文件可以去,但是在config文件夹中移动它会使它从类路径中消失。

2 个解决方案

#1


44  

If you place anything in src/main/resources directory, then by default it will end up in your final *.jar. If you are referencing it from some other project and it cannot be found on a classpath, then you did one of those two mistakes:

如果你在src / main / resources目录中放置任何东西,那么默认情况下它将最终在你的最终* .jar中。如果您从其他项目引用它并且在类路径中找不到它,那么您会遇到以下两个错误之一:

  1. *.jar is not correctly loaded (maybe typo in the path?)
  2. * .jar未正确加载(可能是路径中的拼写错误?)

  3. you are not addressing the resource correctly, for instance: /src/main/resources/conf/settings.properties is seen on classpath as classpath:conf/settings.properties
  4. 您没有正确地处理资源,例如:/src/main/resources/conf/settings.properties在类路径上看作classpath:conf / settings.properties

#2


57  

A cleaner alternative of putting your config file into a subfolder of src/main/resources would be to enhance your classpath locations. This is extremely easy to do with Maven.

将配置文件放入src / main / resources子文件夹的更简洁的替代方法是增强类路径位置。使用Maven非常容易。

For instance, place your property file in a new folder src/main/config, and add the following to your pom:

例如,将属性文件放在新文件夹src / main / config中,并将以下内容添加到您的pom中:

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

From now, every files files under src/main/config is considered as part of your classpath (note that you can exclude some of them from the final jar if needed: just add in the build section:

从现在开始,src / main / config下的每个文件都被视为类路径的一部分(请注意,如果需要,可以从最终的jar中排除其中的一些:只需添加构建部分:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>my-config.properties</exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>

so that my-config.properties can be found in your classpath when you run your app from your IDE, but will remain external from your jar in your final distribution).

这样当您从IDE运行应用程序时,可以在类路径中找到my-config.properties,但在最终分发版中将保留在jar外部。

#1


44  

If you place anything in src/main/resources directory, then by default it will end up in your final *.jar. If you are referencing it from some other project and it cannot be found on a classpath, then you did one of those two mistakes:

如果你在src / main / resources目录中放置任何东西,那么默认情况下它将最终在你的最终* .jar中。如果您从其他项目引用它并且在类路径中找不到它,那么您会遇到以下两个错误之一:

  1. *.jar is not correctly loaded (maybe typo in the path?)
  2. * .jar未正确加载(可能是路径中的拼写错误?)

  3. you are not addressing the resource correctly, for instance: /src/main/resources/conf/settings.properties is seen on classpath as classpath:conf/settings.properties
  4. 您没有正确地处理资源,例如:/src/main/resources/conf/settings.properties在类路径上看作classpath:conf / settings.properties

#2


57  

A cleaner alternative of putting your config file into a subfolder of src/main/resources would be to enhance your classpath locations. This is extremely easy to do with Maven.

将配置文件放入src / main / resources子文件夹的更简洁的替代方法是增强类路径位置。使用Maven非常容易。

For instance, place your property file in a new folder src/main/config, and add the following to your pom:

例如,将属性文件放在新文件夹src / main / config中,并将以下内容添加到您的pom中:

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

From now, every files files under src/main/config is considered as part of your classpath (note that you can exclude some of them from the final jar if needed: just add in the build section:

从现在开始,src / main / config下的每个文件都被视为类路径的一部分(请注意,如果需要,可以从最终的jar中排除其中的一些:只需添加构建部分:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>my-config.properties</exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>

so that my-config.properties can be found in your classpath when you run your app from your IDE, but will remain external from your jar in your final distribution).

这样当您从IDE运行应用程序时,可以在类路径中找到my-config.properties,但在最终分发版中将保留在jar外部。