Web应用程序中的Java Maven引用文件

bgtovc5b  于 2022-12-02  发布在  Java
关注(0)|答案(2)|浏览(136)

I'm trying to reference some files in a Java application that will be copied into a WAR.
I'm new to Maven, but basically the files are

  • Main
  • Java
  • package
  • Class ---- FROM HERE
  • webapp
  • resources
  • folder ---- TO HERE

I've tried a relative paths such as

File source = new File("../../../webapp/resources/folder");

But can never access that folder from the class.
Is there an alternative approach to this?

gpfsuwkq

gpfsuwkq1#

Always remember that Maven's policy is convention over configuration .
That being said, in your case, while using Maven, you need to follow the Maven Standard Directory Structure .
Create a directory structure like src/main/java and put your package(s) in the java folder.
For any resources, create a folder structure like src/main/resources and put your resources in the resources folder.
After you do this, lets say that there is a file named readme.txt inside src/main/resources directory, then you can simply access this file using:
File file = new File("readme.txt");
Maven will always treat src/main/resources as the root for any resources.
Another example is lets say you have a file named hello.txt in src/main/resources/somefolder/hello.txt then you can access it using:
File file = new File("somefolder/readme.txt");

waxmsbnn

waxmsbnn2#

i have done the same thing you are looking for. same i needed to have latest generated class to be replaced from the classes in war file. so that in pom.xml file you will get the path for the class files getting generated. like:

<!--  <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <executions>
              <execution>
                <id>copy-target</id>
                <phase>install</phase>
                <goals>
                  <goal>copy-resources</goal>
                </goals>
                <configuration>
                  <outputDirectory>C:\jboss-as-7.1.1.Final_AMGEN\jboss-as-7.1.1.Final\standalone\deployments\spff.war\WEB-INF\classes</outputDirectory>
                  <resources>          
                    <resource>
                      <directory>${project.build.directory}\classes</directory>
                      <filtering>false</filtering>
                    </resource>
                  </resources>              
                </configuration>            
              </execution>
            </executions>
          </plugin> -->

so in my case it was <directory>${project.build.directory}\classes</directory> here ${project.build.directory} is the variable initialized at application context level and were returning the project directory path.
so you can try this out.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;

public class PiyushDeployAutomation
{
    public static void main(String[] args)
    {   
        ArrayList<Date> al=new ArrayList<Date>();

        File srcFolder = new File("D:\\AMGEN_UI_4_11_16\\spff\\src\\main\\webapp\\assets");   
        //File destFolder = new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs\\tempbe9a67f0e97b8b3d\\spff.war-e793c8e26b8f97be\\assets");
        File destFolder = new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs\\temp4fd3e2919e5f49c6\\spff.war-bee751ebcb10d977\\assets");

        //File srcFolder2 = new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs\\tempbe9a67f0e97b8b3d\\spff.war-e793c8e26b8f97be\\WEB-INF\\classes\\com");
        //File destFolder2 = new File("D:\\AMGEN_UI_4_11_16\\spff\\target\\classes\\com");

        File srcFolder2 = new File("D:\\AMGEN_UI_4_11_16\\spff\\target\\classes\\com");
        File destFolder2 = new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs\\temp4fd3e2919e5f49c6\\spff.war-bee751ebcb10d977\\WEB-INF\\classes\\com");

        //File file = new File("C:\\Windows");
        File file = new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs");
        String[] names = file.list();

        for(String name : names)
        {
            //if (new File("C:\\Windows\\" + name).isDirectory())
                if (new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs\\" + name).isDirectory())
            {
                    System.out.println(name);
                    System.out.println(new Date(new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs\\" + name).lastModified()));
                    al.add(new Date(new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs\\" + name).lastModified()));
            }
                /*List l*/
        }

        //ArrayList al=new Date(new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs\\" + name).lastModified());

        Collections.sort(al);
        Date lastModi=al.get(al.size()-2);
        for(String name : names)
        {
            //if (new File("C:\\Windows\\" + name).isDirectory())
                if (new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs\\" + name).isDirectory())
            {
                    Date d=new Date(new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs\\" + name).lastModified());

                    if(lastModi.compareTo(d)==0)
                    {
                        System.out.println(lastModi.compareTo(d));
                        System.out.println("found and name is ="+name);
                        System.out.println(""+d);
                        System.out.println(""+lastModi);
                        foldername=name;

                    }
            }
                /*List l*/
        }

        File file1 = new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs\\"+foldername+"");
        String[] names1 = file1.list();

        File srcFolder_auto = new File("D:\\AMGEN_UI_4_11_16\\spff\\src\\main\\webapp\\assets");   
        //File destFolder = new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs\\tempbe9a67f0e97b8b3d\\spff.war-e793c8e26b8f97be\\assets");
        File destFolder_auto = new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs\\"+foldername+"assets");

        //File srcFolder2 = new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs\\tempbe9a67f0e97b8b3d\\spff.war-e793c8e26b8f97be\\WEB-INF\\classes\\com");
        //File destFolder2 = new File("D:\\AMGEN_UI_4_11_16\\spff\\target\\classes\\com");

        File srcFolder2_auto = new File("D:\\AMGEN_UI_4_11_16\\spff\\target\\classes\\com");
        File destFolder2_auto = new File("C:\\jboss-as-7.1.1.Final_AMGEN\\jboss-as-7.1.1.Final\\standalone\\tmp\\vfs\\"+foldername+"WEB-INF\\classes\\com");

        //make sure source exists
        if(!srcFolder.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
               copyFolder(srcFolder,destFolder);
               copyFolder(srcFolder2,destFolder2);

           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }

        System.out.println("Done");
    }

    public static void copyFolder(File src, File dest)
        throws IOException{

        if(src.isDirectory()){

            //if directory not exists, create it
            if(!dest.exists()){
               dest.mkdir();
               System.out.println("Directory copied from " 
                              + src + "  to " + dest);
            }

            //list all the directory contents
            String files[] = src.list();

            for (String file : files) {
               //construct the src and dest file structure
               File srcFile = new File(src, file);
               File destFile = new File(dest, file);
               //recursive copy
               copyFolder(srcFile,destFile);
            }

        }else{
            //if file, then copy it
            //Use bytes stream to support all file types
            InputStream in = new FileInputStream(src);
                OutputStream out = new FileOutputStream(dest); 

                byte[] buffer = new byte[1024];

                int length;
                //copy the file content in bytes 
                while ((length = in.read(buffer)) > 0){
                   out.write(buffer, 0, length);
                }

                in.close();
                out.close();
                System.out.println("File copied from " + src + " to " + dest);
        }
    }
}

this program is for JBOSS 1.7 which used to replace all latest created .class,.js and .jps files in war so that after running this program client will get latest updated files in your dev environment. it will reduce the effort of deploying your war files to server again and again because for heavy application Maven clean and install is very time taking task. edit the program as per your convenience.

相关问题