28
votes

I have Maven a pom.xml file (copied from another project, and adapted), and I would like to generate the directory structure for a new project. Which command can do this?

Note that I found countless examples on the web, that explained how to use mvn archetype:create to create a pom.xml file and the directory structure, but I already have a pom.xml, and I would like to create the directories based on that, without specifying groupId, artifactId etc on the command line.

mvn archetype:create-from-project also seems to create a pom.xml based on another pom.xml (?) but I just want the directories. Thank you in advance!

EDIT: I am aware that this is not a "big problem" because I can create the directories manually :) I did it already many times, and I always felt that there must be a smarter way to do it...

8
I use an IDE to build the project structure. It generates the maven poml. from a template and the directories required. It also sets the parent pom when adding modules. - Peter Lawrey
I use Idea, and after "New Project" I select import from Maven. This set up the pom.xml and the libraries, but did not create the maven-speciofic directory structure. - lbalazscs
It only creates the directories if you use it to create the pom.xml as well. - Peter Lawrey
Yes, if I use Idea to create a new project with a "maven module", then it creates the directories, but then I have to specify the groupId, artifactId myself (and copy manually all the dependencies and settings), so this is not easier than mvn archetype:create. - lbalazscs
The problem is that it is not different... Creating archetypes seems overkill to me... I would like more than a solution, I would like a simple solution :) - lbalazscs

8 Answers

18
votes

I agree that there should be a way to specify that maven itself should look at my pom and generate a project/dir structure from that. I don't know of a way to do that, but here's what I do:

Step 1: move your already created pom.xml somewhere else (maven will complain if you already have it in the directory where you will run the next command)

Step 2: from the command line, in your new maven project directory execute:

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DgroupId=my.package.path -DartifactId=myartifact

You do have to edit groupId and artifactId to match your pom (sigh), but this creates the basic directory structure for you and a default Java class and Unit test.

Step 3: move/copy your pom.xml into that project directory

Step 4: run some maven command like mvn clean package or mvn dependency:tree to start downloading dependencies

Note: I'm including this answer because some of the previous answers recommend using mvn archetype:create, but according to the maven website that goal is deprecated in favor of using generate. And I wanted to show how to do it independent of any IDE or IDE plugins.

15
votes

Not to sound condescending, but:

mkdir -p src/main/java
mkdir -p src/main/resources
mkdir -p src/test/java
mkdir -p src/test/resources

(or feel free to substitute different directories).

I don't know of any maven command that will create this structure for you without creating a new pom file.

2
votes

It seems to me like you are making a big problem out of a little one.

I'd try using mvn archetype:create-from-project ... and then just replace the generated POM file with the original one.

And as Peter Lawrey suggests, a lot of modern IDEs are capable of creating a Maven project structure.

2
votes

Analog to Matt's answer these lines will create the default maven folder structure on Microsoft Windows OS. Run them from the directory where your pom.xml exists:

mkdir -p src/main/java
mkdir -p src/main/resources
mkdir -p src/test/java
mkdir -p src/test/resources

I wonder why there is no maven command for this. You can either run the mkdir commands above, or use an archetype instead i.e. quickstart. There is the folder structure already in the box.

0
votes

Use the Maven Build Helper plugin. Usage is pretty straightforward. Just configure in the plugins section of your pom, and call the appropriate mvn goal(s).

From the documentation:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.10</version>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>some directory</source>
                ...
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
0
votes

Maven can generate it for you using the quickstart archetype:

mvn archetype:generate \
    -DgroupId=com.mydomain \
    -DartifactId=my-project \
    -DarchetypeArtifactId=maven-archetype-quickstart

Then, copy and paste your existing pom.xml into the same directory as the pom.xml that was generated.

0
votes

Open command line, change directory to the folder with the pom.xml
and past this into the CMD:

echo This will create all required folders for maven project.
echo creating  Application\Library sources
MD "src\main\java"
echo creating  Application\Library resources
MD "src\main\resources"
echo creating  Resource filter files
MD "src\main\filters"
echo creating  Web application sources
MD "src\main\webapp"
echo creating  Test sources
MD "src\test\java"
echo creating  Test resources
MD "src\test\resources"
echo creating  Test resource filter files
MD "src\test\filters"
echo creating  Integration Tests (primarily for plugins)
MD "src\it"
echo creating  Assembly descriptors
MD "src\assembly"
echo creating  Site
MD "src\site"
0
votes

I use this one-liner:

touch pom.xml && mkdir -p src/main/java && mkdir -p src/main/resources && mkdir -p src/test/java && mkdir -p src/test/resources

It creates also an empty pom.xml. For quick access I've put it in my notes. Beside that it is similar @Matt answer.