如何从Maven项目获取资源

时间:2022-03-21 17:17:03

Maven is very difficult. I would like to reference the resources in my code. But I can't.

Maven很难。我想在我的代码中引用资源。但我不能。

I have got a project set up in Eclipse. In it I have the following structure

我在Eclipse中设置了一个项目。在其中我有以下结构

src
---main
--------java
------------myclasses
-------resources
------------sometextfiles.txt

The relevant part of the pom.xml is as follows:

pom.xml的相关部分如下:

<build>
        <finalName>MyProject</finalName>
        <plugins>

          <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <configuration>
    <filters>
         <filter>
            <artifact>*:*</artifact>
               <excludes>
                  <exclude>META-INF/*.SF</exclude>
                  <exclude>META-INF/*.DSA</exclude>
                  <exclude>META-INF/*.RSA</exclude>
               </excludes>
            </filter>
         </filters>
        <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>main.java.Console.TBB_SQLBuilder</mainClass>
            </transformer>
        </transformers>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>


        </plugins>
    </build>

When I do Maven build clean install from Eclipse usually the target folder doesn't generate the resources folder. A bug which others have suffered from. I think this can be remedied by just creating the resources folder manually in the target folder but that's not very useful. But even if I do this I don't seem to be able to use the resources:

当我从Eclipse构建干净安装时,通常目标文件夹不会生成资源文件夹。其他人遭受的一个错误。我认为这可以通过在目标文件夹中手动创建资源文件夹来解决,但这不是很有用。但即使我这样做,我似乎也无法使用这些资源:

My code:

public class Checkers {
     ArrayList<String> che2=new ArrayList<String>();
     static URL dictionary;
     String n=null;

    public Checkers() {

    }

    public static String FindNReplace(String n) throws IOException, URISyntaxException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(ClassLoader.class.getResourceAsStream("FindNReplaceDictionary.txt")),2048);

String [] split=null;
        ArrayList<String> orig= new ArrayList<String>();
        String [] orig_arr=null;
        ArrayList<String> newDoc= new ArrayList<String>();
        String [] newDoc_arr=null;      

            StringBuilder sb = new StringBuilder();
            String line = reader.readLine();

            while (line != null) {
                split=line.split(":");
                System.out.println("SPLIT"+split);

                                 }

               }

}

I've also tried FindNReplaceDictionary.txt with a leading "/" but no joy.

我也尝试过带有“/”的FindNReplaceDictionary.txt,但没有快乐。

So to deal with my confusion here are my questions 1. How do I reference the resource in Eclipse so that I can run it as a java project at least 2. How do I reference the resource once the Maven project has undergone build clean install 3. Do I need to change anything between running it as a java project and doing a Maven build

所以在这里处理我的困惑是我的问题1.如何在Eclipse中引用资源,以便我可以将它作为一个java项目至少运行2.一旦Maven项目经历了build clean install 3,我如何引用该资源?在作为java项目运行和执行Maven构建之间,我是否需要更改任何内容

Yes I am confused about the whole thing.

是的我对整件事感到困惑。

1 个解决方案

#1


1  

Artifacts in "resources" folder are copied to your final packaged/compiled classpath. In case of web application it will be 'WEB-INF/classes' other instances it will go to 'classes' folder.

“resources”文件夹中的工件将复制到最终打包/编译的类路径中。在Web应用程序的情况下,它将是'WEB-INF / classes'其他实例,它将转到'classes'文件夹。

if you want another resource folder you can specify that in build

如果您想要另一个资源文件夹,可以在构建中指定它

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

You will not be able to change a maven project to eclipse project easily, because maven has standard opinionated folder structure

您将无法轻易地将maven项目更改为eclipse项目,因为maven具有标准的自定义文件夹结构

#1


1  

Artifacts in "resources" folder are copied to your final packaged/compiled classpath. In case of web application it will be 'WEB-INF/classes' other instances it will go to 'classes' folder.

“resources”文件夹中的工件将复制到最终打包/编译的类路径中。在Web应用程序的情况下,它将是'WEB-INF / classes'其他实例,它将转到'classes'文件夹。

if you want another resource folder you can specify that in build

如果您想要另一个资源文件夹,可以在构建中指定它

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

You will not be able to change a maven project to eclipse project easily, because maven has standard opinionated folder structure

您将无法轻易地将maven项目更改为eclipse项目,因为maven具有标准的自定义文件夹结构