Converting to Maven Project from Java Project - eclipse

I have installed m2eclipse plugin. I tried to convert a java project into a maven project.Here is my pom.xml
<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/xsd/maven-4.0.0.xsd">
<groupId>com.test</groupId>
<artifactId>SpringExample</artifactId>
<version>1.0.0</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<dependencies>
<dependency>
<artifactId>commons-lang</artifactId>
<groupId>commons-lang</groupId>
<version>2.1</version>
</dependency>
</dependencies>
</project>
When I clicked on configure and converted it into maven project , I found out that 2 folders have been generated bin & target and all the class files are in bin folder, target folder is empty & jar was not created.
This is the first time I'm trying to use maven and what I understand is that there should have been 2 folders generated src and target.
I can't figure out what went wrong(I did not get any error).
I am using eclipse juno ,I have installed m2e version 1.3.1.x

I would recommend that you read Maven in 5 minutes.
Converting the project will only generate the pom ("This wizard creates a new POM (pom.xml) descriptor for Maven.").
If you want to generate an project with the folder structure you have to use an maven archetype.
Example:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.test -DartifactId=SpringExample -Dversion=1.0.0 -Dpackaging=jar -DinteractiveMode=false

You have to put your code under src/main/java. Tests should be under src/test/java. Your output is in target directory. Bin is generated by Eclipse not maven.
More about directory structure here

Related

Adding a local Maven project as a dependency in another local Maven project

I have a local java application that I have recently converted to a Maven project. My goal is to add my recently converted maven project to another Maven project as a dependency in Eclipse STS.
The temporary path of the recently converted maven project is C:\Users\nconnor2\Desktop and the path of the soon-to-be parent Maven project is C:\Users\nconnor2\Desktop\adoudrepo.
I'm really struglling to wrap my head around how maven works when dealing with local maven project.
The pom of my new maven project is
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TB2TUBSync</groupId>
<artifactId>TB2TUBSync</artifactId>
<version>1.5</version>
</project>
The pom of my soon-to-be parent Maven project is
<?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>com.bellmts</groupId>
<artifactId>test</artifactId>
<name>TB2TUBSyncWeb</name>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
What I have been trying to do is to include my new maven project as a module in my soon-to-be parent maven project but I get an error that I can't include a module in another maven project with war packaging.
At the end of the day I need to access methods from TB2TUBSync (new maven) in TB2TUBSyncWeb (soon-to-be parent maven).
You'll want to leverage the WAR Plugin to generate both JARs and the WAR file, then you can publish the jars to your local repository with mvn clean install. At that point you can import them into the other POM with dependency resolution.
It's detailed at https://eureka.ykyuen.info/2009/10/30/maven-dependency-on-jarwar-package/ .

When automating Eclipse's "Export as Feature", Maven/Tycho doesn't see my plugin

I have a plugin and a feature project in my workspace. When I export the feature manually via File > Export As > Feature everything works well. I'm trying to write an automatic plugin building and exporting script to get rid of this chore. I converted feature project to Maven project and filled pom.xml with:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MyProject</groupId>
<artifactId>NMGDBPluginFeature</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>eclipse-feature</packaging>
<properties>
<tycho-version>0.22.0</tycho-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>eclipse-luna</id>
<layout>p2</layout>
<url>http://download.eclipse.org/releases/luna</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
However script throws:
[ERROR] Cannot resolve project dependencies:
[ERROR] Software being installed: NMGDBPluginFeature.feature.group 1.0.0.qualifier
[ERROR] Missing requirement: NMGDBPluginFeature.feature.group 1.0.0.qualifier requires 'GDBFifoBlocks [1.0.0.gdbfifoblocks]' but it could not be found
How could that happen? I thought pom.xml uses feature.xml of project, doesn't it? What is a proper configuration?
So far, your configuration looks good. However you currently only have an automated build for your feature, but not the for the plugin. Unlike the Eclipse export wizard, eclipse-feature only processes the feature.xml - and it expects that the referenced plugins are built elsewhere.
So what you need to do is to set up a Maven reactor which includes both an eclipse-feature and an eclipse-plugin project. Here is how you do this:
Make your current pom.xml the parent POM: Change the packaging to pom, adapt the artifactId to something which makes sense (e.g. MyProject.parent), and move the pom.xml into a new general project in your workspace.
Add a pom.xml in the root of the feature project:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>MyProject</groupId>
<artifactId>MyProject.parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>relative/path/to/parent/project</relativePath>
</parent>
<artifactId>NMGDBPluginFeature</artifactId>
<packaging>eclipse-feature</packaging>
</project>
Add another pom.xml in the root of the plugin project, which is the same as the one above except for the artifactId - this needs to be the same as the plugin's Bundle-SymbolicName - and the packaging which needs to be eclipse-plugin.
Include the plugin and feature projects in the Maven reactor by adding a <modules> section in the parent POM with the paths to these projects:
<modules>
<module>relative/path/to/plugin/project</module>
<module>relative/path/to/feature/project</module>
</modules>
Note that the paths need to be adapted so that they are correct for the project locations on disk (which may be different to what is shown in the Eclipse workspace). The paths need to be relative, so they probably start with ../.
Now you can trigger a Maven build on your parent POM, and the feature should be able to resolve the reference to your plugin. In Eclipse, you can trigger the Maven build from the context menu of the pom.xml file. Or, if you also convert the parent project to a Maven project, the you can also run Maven builds from the context menu of the project root.

Deploying dependent (external) war in in eclipse wtp project

Its been a while since I used eclipse. I have a webapp that is dependant on another .WAR (geoserver.war). I use eclipse Juno J2EE package, and what I'm failing to configure is to have tomcat deploy my dependant .WAR along with the project built webapp.
I've ended up copying geoserver.war to ..\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\webapps, so now its deployed whenever I run the app on the server, but this is obviously not the proper way.
How to configure eclipse tomcat integration to deploy the external war as well.
I had a similar problem and and I fixed using WTP and Maven.
What I did is to create a dummy war project with a single dependency that is the war that I wanted to use.
Using this technique, the imported war would be used as an overlay war and, because of the project, eclipse will allow you to add it as a module to a server configuration.
In details, the pom will look something like:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>your-module-name</artifactId>
<parent>
<groupId>your.group.id</groupId>
<artifactId>your.parent</artifactId>
<version>your.version</version>
</parent>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>war.group.id</groupId>
<artifactId>war.id</artifactId>
<type>war</type>
</dependency>
</dependencies>
</project>
After that, you import it as an existing maven project and voila`, the war is now available as a module to add to any server configuration.

Eclipse with maven workspace dependency - packaged war contain folder instead of jar

I'm using Eclipse Kepler SR2 with m2e.
I have a web project that depends on a jar.
When I use "Run as -> Maven Build..." with goal package and "Check Workspace artifacts" is checked, then the lib folder in the target contains a folder with the name of the dependency jar, instead of the jar itself. The packaged war also contains a folder instead of a jar. However, when deploying to tomcat with m2e, the jar is deployed correctly.
I use maven-war-plugin version 2.4.
This is my WAR pom:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.modelity.loans</groupId>
<artifactId>loans-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>loans-web</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.modelity.loans</groupId>
<artifactId>loans-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</build>
</project>
And this is the artifact coordinates of the dependency jar:
<parent>
<groupId>com.modelity.loans</groupId>
<artifactId>loans-lib</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>loans-core</artifactId>
<packaging>jar</packaging>
I've looked at the debug output of the maven package goal. When the war plugin is processing the jars, it says
[DEBUG] Processing: loans-core-0.0.1-SNAPSHOT.jar
without indicating the jar has been copied.
Later on in the log I see:
adding directory WEB-INF/lib/loans-core-0.0.1-SNAPSHOT.jar/
I've tried both embedded maven (3.0.4) and external maven (3.0.5).
My colleague uses kepler SR1, and experience the same problem.
I think I've covered it all. Would be happy for some advise, couldn't find any reported bug about it.
Thanks,
Lior
Try installing the jar as its own maven-controlled artifact:
mvn install:install-file -Dfile=my.jar -DgroupId=com.mycorp -DartifactId=my-jar -Dversion=1.0 -Dpackaging=jar
And then define it as a maven dependency for your war
now we have 2018 and the problem still exists in eclipse v2018-09. Maven Dependencies which exist in the workspace are exploded into WEB-INF/lib folder.
Found this old bug report https://issues.jboss.org/browse/JBIDE-22157 regarding this behaviour.
Solution is to disable "dependency resolution from workspace projects" in maven project settings.
regards,
Markus

maven "Please verify you invoked Maven from the correct directory" error in eclipse

I created a java project in eclipse. The project location is /home/vikas/code/primeNumberWorkSpace/primeNumber.
I linked the source and pom files from other locations (/home/vikas/code/primNumber/src/ and /home/vikas/code/primeNumber/pom.xml)
When i issue 'clean install' command from the eclipse, it gives the following error,
[ERROR] The goal you specified requires a project to execute but there
is no POM in this directory
(/home/vikas/code/primeNumberWorkSpace/primeNumber). Please verify you
invoked Maven from the correct directory. -> [Help 1]
How to resolve this.
the pom file is:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>main.java.com.simpragma.primenumber</groupId>
<artifactId>prime-number</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
</build>
</project>
You might have tried setting the AutoRun registry key for Command Processor to change where cmd.exe has its starting location...
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\AutoRun
Check this value, as a CD in this autorun will change the default location of all cmd.exe that are run on the system. Which is wonderful, but it apparently breaks something in maven workflow.
Deleted my AutoRun value and this issue went away for me.
I was also having this issue with maven projects within MuleSoft Anypoint Studio 6.4.4, which is based on Eclipse 4.5.2. By removing the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\AutoRun value the issue went away.
Thanks for your answer Dave Horner.