Tim Holloway wrote:You missed Basic Maven, then.
I'll admit one of the things that kept me using Ant for a long time was that I didn't want to have my project structure dictated to me.
I got over it. It's nice that anyone anywhere can hand me a standard Maven project and I'll know where to thing things and how to build it.
The primary directories in a Maven project are under 2 main trees. The /src directory contains the application and unit test directories (/src/main and /src/test, respectively)
Under each of those directories are language-specific directories for source code and one or 2 directories for non-compiled resources.
The other main tree is /target. That's where the intermediate and final files are placed when you build. It can be safely deleted, and a "mvn clean" goal will do exactly that, ensuring that only the essential project resources remain in case you want to do a complete from-scratch build or ZIP up the project and send it somewhere.
OK. Let's drag out the old File Manager and rearrange your project to run under standard Maven. I'll use "com.coderanch.example" as my master package root, so that will give me these directories:
Stuff moved from your /model directory
At that point Maven will build wnat you want without you having to do any special coding in the POM to locate files.
Incidentally, here's what a project of mine keeps under /src/main/java/com/mousetech/gourmetj:
top-level classes for the app
The 3 persistence subdirectories reflect my standard way of implementing JPA, with top-level persistence in the service package, per-table DAOs in the dao package and the Entity Models in the model package. This app is based on JavaServer Faces, but also uses Spring Web for functions that JSF isn't good at. Normally I'd have a subdirectory for the backing beans as well, but this was an experimental project so I just dumped them in the top level app class directory.
Tim Holloway wrote:Your picture does not display.
But your test doesn't appear to be correct. Source code must be in package-structure form under /src/main/java for app source and /src/test/java for test code.
You seem to have placed your code under /main, not /src/main, and unless your package names really are "app" and "logic", that part is wrong also. Recommended package names resemble bottom-up Internet domain names, so for example, coderanch.com gives a recommended package path of com.coderanch and thus a Maven source code directory structure of /src/main/java/com/coderanch (or for Windows, \src\main\java\com\coderanch.). Package name components should be all lowercase characters.
If you don't have an appropriate commercial domain name, there are other conventions. For example, edu.ucf.cop0121.tholloway.assignment6. We can give you more guidance on package structure if you need it, but package structure requirements for Java are the same regardless of whether you use Maven or not. Maven only pre-determines the base of the package source.
Tim Holloway wrote:Hmmm. Just looked at your POM and it is not set up to build a WAR. You need this:
Put it somewhere around your artifactId.
You probably need this added to your plugins:
Version number may need to change - I got that from an old project.
Tim Holloway wrote:1. Remove that sourceDirectory stanza from your POM.
2. Make sure that you have the packaging stanza value set to "war"
3. Tell Maven to build for the "war" goal. "war:war" indicates a WAR file which will be placed in the target directory. There are other "war" sub-goals but that one is best to start with.
Tim Holloway wrote:Remove all of this:
You are building a webapp, so you should not be building a JAR, you should be building a WAR. Exportable or not.
I don't recommend using your IDE to run Maven here until you understand it from the command line. The IDE will make more sense when you do.
Also, run the "mvn clean" goal to get rid of any dirt in the project before you build the WAR.
Tim Holloway wrote:Sounds OK to me, but it may indicate you need a newer version of the WAR plugin.
Originally, you needed a web.xml file (technically known as container-independent deployment descriptor) to configure your webapp and so not having one (ideally as /src/main/webapp/WEB-INF/web.xml) would indicate a defective build configuration. But for many years now, the web.xml file has not been necessary since the information it contained can be extracted from annotations on the source code. So these days the only time it should be complaining is if there is neither web.xml nor annotations.
Tim Holloway wrote:
Jannik Joos wrote:
Okay, then what do I do now since the war file is created? : D
Deploy it to a webapp server and test it. Which webapp server — Tomcat, jetty, Wildfly, WebSphere, WebLogic, whatever — that's your choice. We have forums to help with all of those.
Tim Holloway wrote:It certainly looked like a webapp to me. That's why I told you how to build it as a webapp.
Tim Holloway wrote:Now that you mention it, it has a class named "Main". Sorry.
Change the "packaging" from "war" to "jar", remove the failOnMissingwebxml. then build:
mvn clean compile jar:jar
This isn't a self-executing JAR, I don't think, so you'd have to run it using something like "java -jar target/BMAnalyze-0.0.1-SNAPSHOT.jar" and you may need to give the additional argument "app.Main" if you don't have a META-INF Main-Class directive in the JAR you build.
Tim Holloway wrote:The JAR should not contain a src or main folder. The top-level folders should be "app" and "logic" for the classes. For a JAR, the classpath root is also the root of the JAR, unlike WARs, which put their classes under WEB-INF/classes.
If that is not what you are seeing, post an updated copy of your POM (with code tags).
Note that unless you build a self-executing JAR (which requires extra Maven plugins), any dependency JARs copied into the target JAR will not be usable by the application. That is a Java limitation, not a Maven one.
Tim Holloway wrote:OK. Eventually I'll manage to see what's in front of me (I hope!). That does look like a project for a self-executing JAR.
One thing I can't quite tell though is it looks like your POM is inside the src directory. That's not what it should be. The POM should be in the project root along with the src and target directories. You shouldn't need the bin directory any more.
Since you are using Exlipse as your IDE and Eclipse will automatically compile classes when you edit them, you do need to adjust the Project Properties. In Eclipse, you can set the source and test source directories as well as the class output directories to reflect where Maven expects them. Eclipse won't automatically do that itself if you created the project as a non-Maven project.
Ron McLeod wrote:I've read through the previous posts, but it is still not clear (to me). Do you have a directory under src named main.java.app, or do you have a directory named main which contains a directory named java which contains a directory named app?
Jannik Joos wrote:The actual folder is the second what you said, src/main/java/app
Ron McLeod wrote:
Jannik Joos wrote:The actual folder is the second what you said, src/main/java/app
Did you verify by checking the in the file system?
I have an older version of Eclipse, but this is what I would expect, and what I do see:
Tim Holloway wrote:A note on Eclipse icons. The folder-with-a-grid icon indicates a source directory. A grid-only icon indicates a package. Ron's icons show how Eclipse needs to be configured, but as I said, that's not the default for basic (non-Maven) Eclipse projects, so you'd need to edit project properties to set the source and test folders as required as well as the compiled classes folder(s).
Tim Holloway wrote:Yes, but you just undid all the advice we gave, didn't you? "/src" is what Eclipse uses as a default when it's a non-Maven project.
Tim Holloway wrote:IDK why but it does is a rather precarious position, though. It basically means "it's likely to break again and I still won't know how to fix it".
If it works, fine, but just for giggles I recommend that you also take the standardized Maven project setup we've been suggesting and see if you can't get it working just so you'll understand it better. You never know when Elon Musk may make a big-money offer to you if you can get some Maven-based Twitter components working again.
And don't forget that screenshot I posted about how Eclipse wants its source and class directories configured!