4

Is there a way to use Maven to build Java projects without all the unnecessary directories?

https://spring.io/guides/gs/maven/

It looks like Maven expects

src/
  main/
     java/
        package1/
        package2/
        package3/
        packageX/

I would like to create a Maven project with a pom.xml file, that looks more like:

src/
  package1/
  package2/
  package3/
  packageX/

Is this possible?

Here is the pom.xml file I am using:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.springframework</groupId>
   <artifactId>gs-maven</artifactId>
   <packaging>jar</packaging>
   <version>0.1.0</version>
   <build>
      <sourceDirectory>src</sourceDirectory>
      <plugins>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
               <execution>
                  <goals>
                     <goal>java</goal>
                  </goals>
               </execution>
            </executions>
            <configuration>
               <mainClass>hello.HelloWorld</mainClass>
               <arguments>
                  <argument>argument1</argument>
               </arguments>
               <systemProperties>
                  <systemProperty>
                     <key>myproperty</key>
                     <value>myvalue</value>
                  </systemProperty>
               </systemProperties>
            </configuration>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.1</version>
            <executions>
               <execution>
                  <phase>package</phase>
                  <goals>
                     <goal>shade</goal>
                  </goals>
                  <configuration>
                     <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                           <mainClass>hello.HelloWorld</mainClass>
                        </transformer>
                     </transformers>
                  </configuration>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>
</project>
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 2
    Those directories aren't unnecessary. Where are you putting your `test` classes and your `resources`? – chrylis -cautiouslyoptimistic- Mar 26 '17 at 00:19
  • 1
    You should really write unit-tests, though. So, `src/test/java`. And resource files? `src/main/resources`... So, they are not "unnecessary". – OneCricketeer Mar 26 '17 at 00:19
  • Why the downvote, not everyone uses Java for the same purpose, I am attempting to use it for more of a scripting purpose, not a full application. I don't need the ceremony or the cruft if I can avoid, all the nested directories just make things less friendly for the human - you! – Alexander Mills Mar 26 '17 at 00:23
  • 2
    I agree that the directories aren't unnecessary. I have found it grossly simpler to write tests, include resources, build assemblies and manage multiple source languages when using the recommended format for Maven. These things generally collapse in a nice IDE like Eclipse anyhow, and the fewer custom configurations you have the easier the project is to maintain. – Steve Harris Mar 26 '17 at 00:39
  • @Steve that is fear talking! The fear of configuration hell. But this is a very very simple thing. Maven should be able to handle pointing to a different directory. That is all :) – Alexander Mills Mar 26 '17 at 00:44
  • 1
    No, it is observing standards to improve maintainability. it is years of experience trying to do it "my way" only to be confronted by the same problems that came before me. It isn't "fear", I'm not afraid to configure. By why are you saying that added "directories" are cruft, but added config is not? If you are scripting and you aren't afraid of configuration, I suggest the assembly plug-in. With it you can build assemblies that are clean and still have your project maintain standards that are default configs. – Steve Harris Mar 26 '17 at 00:46
  • Or just use the Groovy command-line runner. – chrylis -cautiouslyoptimistic- Mar 26 '17 at 01:16
  • @SteveHarris I understand your argument, I am simply trying to discover a sustainable way to create maven projects that are simpler in structure. To be afraid (or whatever you want to call it) to change the configuration, means you just have too much experience with computers ;) – Alexander Mills Mar 26 '17 at 01:18
  • If you use a current IDE that is well integrated with Maven, the change in source directory configuration should reflect in the pom automatically. But I'm not afraid of config - just saying that if you submitted code that changed the standard on my watch, I would kick it back in review to be changed back. Just sayin' – Steve Harris Mar 26 '17 at 01:29
  • Right, if on a team project, it's best to stick to the standard for multiple reasons. I am creating OSS and want to demonstrate to users how to write tests in Java that are packaged by maven without having to create a whole application structure typical of a "normal" maven project. AKA, you write a script but you want all that shit to go with the script? No, you don't. I used to work with Java a bit, but frankly, things do not work with Java unless you have an IDE and like 50 tools. Not a fan anymore. Only a fraction of people that work with Java daily really know the command line. – Alexander Mills Mar 26 '17 at 01:55
  • In other words, people keep telling me use Eclipse to build Java or use Maven or Gradle to build Java. I just want to build Java with jar and javac, goddamnit! – Alexander Mills Mar 26 '17 at 02:00
  • I hope no one else had to work on a project you have written. Why do it the same as everyone else, when i can do it differently for no benefit. – dan carter Mar 19 '20 at 23:26

2 Answers2

9

I suggest you follow the src/main/java structure because of the benefits (look here) it provide, but in case, if you have to change the folder structure for any other reasons to src (or any folder), then you can do that by specifying the sourceDirectory in your pom.xml as shown below:

<build>
    <sourceDirectory>src</sourceDirectory>
</build>
Community
  • 1
  • 1
Vasu
  • 21,832
  • 11
  • 51
  • 67
  • nice - I tried it and it didn't work - but I am guessing because I am doing something wrong, and that this methodology is ok – Alexander Mills Mar 26 '17 at 00:27
  • Can you add your `pom.xml` file so that I will have a look? – Vasu Mar 26 '17 at 00:31
  • Maybe this is more correct? http://stackoverflow.com/questions/19140873/change-source-directory-in-profile-maven – Alexander Mills Mar 26 '17 at 00:31
  • Ok I will share my pom.xml file, because the other answer didnt work either – Alexander Mills Mar 26 '17 at 00:34
  • Can you tell me what is the issue i.e., exactly is not working? what happens when you try `mvn clean install`? because your pom picks the src files and runs fine in my system – Vasu Mar 26 '17 at 00:39
  • "mvn clean" works, but "maven package" fails with an error. The error is a simple result of the fact that maven is still looking to src/main/java/hello/HelloWorld instead of src/hello/HelloWorld. – Alexander Mills Mar 26 '17 at 00:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139060/discussion-between-javaguy-and-alexander-mills). – Vasu Mar 26 '17 at 00:42
3

The Maven docs state that it is a recommendation and there are ways around it via the project descriptor if the layout is no option.

Please try to conform to this structure as much as possible; however, if you can't these settings can be overridden via the project descriptor.

ldz
  • 2,217
  • 16
  • 21