Petr Mensik
Petr Mensik

Reputation: 27496

How to create new directory in target folder with Maven

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.

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/myLibrary after the build.

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?

Upvotes: 4

Views: 21108

Answers (2)

Petr Mensik
Petr Mensik

Reputation: 27496

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

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);

Upvotes: 2

carlspring
carlspring

Reputation: 32607

In your library, simply check if the target directory (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-plugin is doing.

Upvotes: 0

Related Questions