Java 如何使用Maven在目标文件夹中创建新目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18633956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:41:51  来源:igfitidea点击:

How to create new directory in target folder with Maven

javamavendependencies

提问by Petr Mensik

I am writing a library which will create a report based on the test results (imagine something like Surefire). And my problem is, I am able to create a folder in target/directory to hold the report and also copy there necessary files but I can do that only when I build the library project itself.

我正在编写一个库,它将根据测试结果创建一个报告(想象像 Surefire 这样的东西)。我的问题是,我能够在target/目录中创建一个文件夹来保存报告并复制必要的文件,但只有在我构建库项目本身时才能这样做。

I would like to achieve same behavior like a Surefire plugin has, that means if I put dependency for my library to some project, let's say myProject, then I will get something like myProject/target/myLibraryafter the build.

我想实现与 Surefire 插件相同的行为,这意味着如果我将我的库依赖于某个项目myProject,那么我会myProject/target/myLibrary在构建之后得到类似的东西。

btw, this is what I currently have

顺便说一句,这就是我目前拥有的

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-antrun-plugin</artifactId>
     <version>1.7</version>
         <executions>
             <execution>
                <phase>generate-test-sources</phase>
                <configuration>
                    <tasks>
                        <echo message="Creating folder for the lib..." />
                        <mkdir dir="${report.folder}" />
                        <echo message="Copying Twitter Bootstrap libraries to the ${report.folder}" />
                        <copy todir="${report.folder}">
                                <fileset dir="src/main/resources/bootstrap">
                                    <include name="**/**" />
                                </fileset>
                            </copy>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

And also bonus question, is there a variable for src/main/resources?

还有奖金问题,是否有变量src/main/resources

采纳答案by Petr Mensik

Ok, so I found the solution which is using the getResourceAsStream()method.

好的,所以我找到了使用该getResourceAsStream()方法的解决方案。

String classpathName = "/bootstrap/" + "css" + "/" + file;
InputStream inputStream = SetupReportDirectory.class.getResourceAsStream(classpathName);
String path = "target/myLibrary/bootstrap";
FileOutputStream outputStream = new FileOutputStream(path);
IOUtils.copy(inputStream, outputStream);

回答by carlspring

In your library, simply check if the targetdirectory (and/or whatever sub-directory you're interested in) exists. If it doesn't just use File.mkdirs(...)and create it. That's what the maven-surefire-pluginis doing.

在您的库中,只需检查target目录(和/或您感兴趣的任何子目录)是否存在。如果它不只是使用File.mkdirs(...)和创建它。这就是它maven-surefire-plugin正在做的事情。