How to build war using maven in Intellij Idea - java

That is pretty easy in Ultimate Itellij Idea.
In community version I have the following problems:
I can't open maven lifecycle window.
When I right click on pom.xml and choose Maven I see only
Using artifacts + build it's impossible to choose .war
It's worth saying that I have mvn project and I need to create .war from one of the subprojects

Are you sure you can't enable the Maven Tool window?
I'm using the Community Edition (2018.3, so not the latest one) and the Maven tool window is available (appears by default on the right), see below:
If you can't find it, try View/Tool Windows/Maven

You can achieve doing cmd into your project root folder and run the maven goal mvn clean install or mvn clean package
Before that make sure you define your project with as packaging of war type and also check you have maven installed and set maven home
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>war</packaging>

Related

Eclipse maven build does not install dependencies

I'm working in eclipse on a java / javascript project, we're using maven to manage our project dependencies, I ran into a issue with maven build (clean install) does not install the dependencies that I specified in the POM.xml file, I tried everything I can find on the internet the whole day today, still no luck, I'll be really appreciate if anyone could kindly take a look at, thank you :)
I got a reuse lib project (lib) and a working project (project), the project should be installing the lib during the maven build, So, here's more detail information on what exactly I did:
I ran maven clean on the lib, then ran maven package, the lib-1.0.0-SNAPSHOT.jar file was generated successfully.
then in the working project POM.xml file, I added the dependency declaration.
I right clicked the working project, maven -> update maven project, where I checked the 'force update of snapshots / releases' checkbox, then -> ok
I right clicked the working project, run as -> maven build (with clean install, and also I checked on the 'Update Snapshots' checkbox) -> apply -> run
In the console, I saw the reuse lib was downloading and then downloaded, but it's never got installed (there should be a line says installing reuse lib...), as the result of it, the reuse lib will not be loaded after I ran my working project, it drives me insane -_-!..
Only your maven projects build output can be installed (in the local repository with mvn install, resp. mvn clean install). During the build it will resolve the dependencies (and the transitive dependencies) to be downloaded and packed to your delivery. Those dependencies of your project will implicitly also be "installed" in your local repository since you will see them in your local repository after the download happened - Maven will however not see that as a install in the meaning of install of the default lifecycle.
To install your "reuse lib"-Maven project you will have to run mvn install or mvn clean install on that project's pom rather than on a project which "just uses it as dependency".

What do I do with the pom.xml file?

I have just downloaded a project from github, it has a pom.xml and is missing many dependencies.
I installed m2e Maven integration in Eclipse plugin but now what do I do? It's installed and seems to be working but how do I download the dependencies now?
You checkout the git files to a location different from your Eclipse workspace.
Start Eclipse on a clean workspace and install m2e. (I usually install m2e-wtp to get web projects)
Use File -> Import -> Existing Maven projects.
Point to the pom.xml you want to use in the files checked out above.
Wait while Eclipse downloads half the Internet.
Ready.
Right click on your project, run as -> maven install.
If you can't see these, you either incorrectly installed m2e, or you need to import the project as a maven project.
In that case, delete the project from your workspace (do not check the "delete contents on disk"). Right click in the project explorer and click on import... From that dialog, you can select existing maven projects, where you need to navigate to the highest directory containing a pom.xml file.
Also, if you have maven installed on your computer (not m2e in eclipse), you can open the directory that contains the pom.xml and just type mvn clean install. This will build your project.
If you get a "Build success" message, you'll know that the dependencies have been fetched correctly.
In eclipse, You have to convert your project to a maven project. right click on your project -> maven (or configure) -> convert to maven project (or enable dependencies management).
This should update your dependencies automatically, if it is not the case then right click your project -> maven -> update project (or update dependencies, it depends on the verion of m2e you are using).
It should be enough to build the project. The dependencies should be downloaded during the build.
Pom.xml files are used for Maven projects.
Depending on your Eclipse version, maybe it's not compatible with Maven out of the box. If not, you can install a Maven plugin (like m2e) so that the project can be viewed as a Maven project.
Maven will do the dependency management (if pom.xml file is configured properly)
When you right click on your project->Run As-> Maven install it will download all the dependencies that it does not have already in your local repository.

How do I manually add a project to the build path of another project?

I have a maven project which won't compile due to an unresolvable reference from another project's artifact.
When I run eclipse:eclipse and open it up it shows an auto fix suggestion of "Add project 'project2' to build path of 'project1'". If I click this everything works. So project1 can clearly see my project2 reference, but doesn't quite use it as expected.
However, once I delete all the files generated for eclipse the error resumes because whatever reference eclipse created has been removed. How can I get this project2 in the project1 build path manually. I already have it listed in the pom as below:
<dependency>
<groupId>group.id</groupId>
<artifactId>project2</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
In "Project properties > Maven" menu you should check "Resolve dependencies from Workspace projects".
When I run eclipse:eclipse[...]
This sounds like you are using the Maven Eclipse plugin. You should probably rather use m2e, which is the recommended way to integrate with Maven now. This will also obviate the need to run mvn eclipse:eclipse. Instead, just import your Maven project into Eclipse, and it will pick up everything automatically.
After you set "Resolve dependencies from Workspace projects" as described in polypiel's answer, things should just work (TM).
If you have maven nature to both your projects, you should do mvn clean install to both the projects, so they will be installed in your local repository and will be available for other projects to use as dependencies. Or just right click to both the projects in eclipse and choose Run As --> Maven install

Maven in eclipse Indigo, project dependency on Runtime

I have a maven project in Eclipse which has as a dependency another project which I've installed in my local repo with mvn clean install. The dependency in pom:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>mydependency</artifactId>
<version>1.0.0</version>
</dependency>
When I run the project from eclipse in tomcat, Run On Server, it runs fine If I don't have the other project loaded in eclipse(mydependency). If I import mydependency as a maven project in eclipse(I need to for debugging purposes), the project fails on runtime with ClassNotFoundException. It cannot find classes of mydependency project.
What am I doing wrong?
P.S. I've tried the usual, clean, delete, reimport projects, switch workspaces, delete/recreate tomcat in eclipse, even switched eclipse installation...
First solution:
Choose on your project right-click-> Maven -> Disable workspace resolution
Now when you will debug the debugger will find the jar file - not the source. You has to attach source code to the library (choose java project)
Second solution
Run your project on tomcat as war file and debug remotely.
Third solution
Install JD-Eclipse . It is helpful, but sometimes the lines of code are not the same as decompiled.

How to configure Eclipse build path to use Maven dependencies?

I would like to take advantage of the features that Maven provides for managing dependencies in a project. My brief understanding of how Maven works is that it will aquire the JARs needed and then build the project with these libraries.
Currently I have a simple POM file set up as a test:
<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>com.jamesgoodwin.test</groupId>
<artifactId>com.jamesgoodwin.test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.0.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
To manage dependencies, I usually add the project or JAR to the build path and then i'll be able to build my project..
But when using M2Eclipse the dependencies are not added automatically to the build path. Is there any configuration to let Eclipse know that the Maven is managing the dependencies?
If you right-click on your project, there should be an option under "maven" to "enable dependency management". That's it.
if you execute
mvn eclipse:clean
followed by
mvn eclipse:eclipse
if will prepare the eclipse .classpath file for you. That is, these commands are run against maven from the command line i.e. outside of eclipse.
I'm assuming you are using m2eclipse as you mentioned it. However it is not clear whether you created your project under Eclipse or not so I'll try to cover all cases.
If you created a "Java" project under Eclipse (Ctrl+N > Java Project), then right-click the project in the Package Explorer view and go to Maven > Enable Dependency Management (depending on the initial project structure, you may have modify it to match the maven's one, for example by adding src/java to the source folders on the build path).
If you created a "Maven Project" under Eclipse (Ctrl+N > Maven Project), then it should be already "Maven ready".
If you created a Maven project outside Eclipse (manually or with an archetype), then simply import it in Eclipse (right-click the Package Explorer view and select Import... > Maven Projects) and it will be "Maven ready".
Now, to add a dependency, either right-click the project and select Maven > Add Dependency) or edit the pom manually.
PS: avoid using the maven-eclipse-plugin if you are using m2eclipse. There is absolutely no need for it, it will be confusing, it will generate some mess. No, really, don't use it unless you really know what you are doing.
Add this to .classpath file ..
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
Thx
Right click on the project Configure > convert to Maven project
Then you can see all the Maven related Menu for you project.
I did like this..
Right click on the project--> configure-->convert to maven project.
Right click on the project-->maven-->add dependencies.
In Eclipse (Mars) go to:
Project Properties --> Maven. Ensure that "Resolve dependencies from workspace projects" is disabled.
I did not found the maven or configure menus but found the following button that solved my problem:
When m2eclipse is installed properly, it should add dependencies automatically.
However, you should generate the eclipse project files by entering:
mvn eclipse:m2eclipse
or, alternatively if you don't use m2eclipse:
mvn eclipse:eclipse
I had a slight variation that caused some issues - multiple sub projects within one project. In this case I needed to go into each individual folder that contained a POM, execute the mvn eclipse:eclipse command and then manually copy/merge the classpath entries into my project classpath file.
For newer Eclipse versions (>=Mars) right click on project > configure > convert to Maven project
Adding my answers for a couple of reasons:
Somehow none of the answers listed directly resolved my problem.
I couldn't find "Enable dependency management" under Maven. I'm using Eclipse 4.4.2 build on Wed, 4 Feb 2015.
What helped me was another option under Maven called as "Update Project" and then when I click it this window opens which has a checkbox that says "Force update of Snapshot/Releases". The real purpose of this checkbox is different I know but somehow it resolved the dependencies issue.
None of that solved my problem. but what I did was if click on the pom.xml, there is a tab at the bottom named dependencies. in this tab it is split into 2 section, one called dependencies and one called dependency management. select every thing in the dependency section and click add to be under the dependency management control. close and reopen your project.
Maybe you could look into maven-eclipse-plugin instead of M2Eclipse.
There you basically add maven-eclipse-plugin configuration to your pom.xml and then execute mvn eclipse:eclipse which will generate the required .project and .classpath files for Eclipse. Then you'll have the correct build path in Eclipse.
Make sure your POM follows the naming convention, and is named in lowercase lettering as pom.xml and NOT POM.xml.
In my case all was right, but Eclipse still complained when trying to Right-click and Update project configuration - told me that the POM could not be read. Changed the name to lowercase - pom.xml from POM.xml - and it worked.
I met this issue too. When I add dependencies in the pom.xml, I checked in the local folder /Users/xyz/.m2/ and the jars are already downloaded there, but cann't added the the buildpath of the eclipse.
My eclipse Version: Mars.2 Release (4.5.2)
right click project > Maven > Enable Workspace Resolution
And this solved my issue.
Typically, you can copy the .classpath file from a working project since there isn’t anything project specific here assuming you’re structured as a standard Maven project.
This worked for me in Eclipse Oxygen (4.7.0):
Right click Project -> Maven -> Select Maven Profiles... then check the Repository Proxy box, press OK.

Categories

Resources