How to create simple maven java web project - java

I spend more than a half day to figure out how to do simple maven project. Eclipse seems weird IDE throwing all errors.
Steps I did:
1, Installed Eclipse
2, Maven home & java home and path set environment variable
3, Create a simple maven project
pom.xml has error first line
[![enter image description here][2]][2]
<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">
From tutorials and videos i've referred it has java resource folder with version mentioned also there when creating new maven project.
I am using eclipse photon and maven 3.5.4
Error dialog : Probel occured
'Building workspace encountered problem'
'Errors occured during build'
Error Dialog description:
Errors running builder 'Maven Project Builder' on project 'TestApp'.
Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.6
pom.xml contents:
<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>com.gps</groupId>
<artifactId>TestApp</artifactId>
<version>0.0.1-SNAPSHOT</version>

<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>com.gps</groupId>
<artifactId>TestApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
This is the very minimum pom.xml content you need. If you have this then the something else should be the problem. You don't need more than this to build a simple basic maven project. I think closing tag </project> is missing the the code you posted

<h4>Error was thrown Because it was unable to download any artifacts or resources from repo.
Reason was i was behind the proxy i needed to configure in settings.xml <br/> Located in <br/> pref-> maven -> user settings<br/>
After i configured everything working fine<br/>
</h4>
<xmp>
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>proxy.somewhere.com</host>
<port>80</port>
</proxy>
</xmp>

Related

Create maven archetype with custom params using requiredProps and fileSets

I need to create a new maven archetype where project files contains some custom params that I would like to provide for mvn archetype:generate. Only -DgroupId, -DartifactId and version works fine. But I cannot provide e.g. -Dparam1=value1 .
I tried using requiredProperties and fileSets, but it doesn't work. How can I achieve that ?
archetype.xml
<archetype xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0 http://maven.apache.org/xsd/archetype-1.0.0.xsd">
<id>my-archetype</id>
<requiredProperties>
<requiredProperty key="param1">
<defaultValue>lol</defaultValue>
</requiredProperty>
</requiredProperties>
<fileSets>
<fileSet filtered="true" encoding="UTF-8" >
<directory>src/main/</directory>
</fileSet>
</fileSets>
<resources>
<resource>src/main/someFile.txt</resource>
</resources>
</archetype>
src/main/someFile.txt
I am just a txt file, but I need ${param1} value to be here !
I also need the pom.xml of my project (the one that is generated when mvn archetype:generate) to have custom properties replaced.
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}-server</artifactId>
<version>${version}</version>
<packaging>pom</packaging>
<name>This name has to contains ${param1} !</name>
// ... all the rest stuff
</project>
Per the archetype descriptor documentation the archetype descriptor needs to be named archetype-metadata.xml. The example shows archetype.xml.
Also note, the initial XML element is archetype-descriptor, not just archetype.
Files must also be in a well-defined location for the plugin to find them.
<yourArchetypeProject>
src
main
resources
archetype-resources
<files and directories for project>
META-INF
maven
archetype-metadata.xml
The Maven docs could definitely be clearer on this. I found this tutorial helpful when I was learning.

Maven - How to create a pom.xml for java project through command line

Currently working on a work training on Maven. I've never used it before and the training is very limited. One of the assessments is asking me to do the following using command line.
Create a new Directory in a local storage.
Place the downloaded project in this directory and create maven build file(pom.xml).**
In pom.xml file create a project element, In project element create default elements such as modelVersion,groupId,artifactId,packaging,version,name for project
Its the number 2 I can't seem to get write. I know how to create a new maven project using command line(below), however when I try to create the pom.xml for a project, it doesn't work.
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Any help would be great thanks.
I solved a similar problem by creating an empty Maven project, then taking that default (basically a template) pom.xml and modifying it to my project's configuration.
What I did was start by creating a pom.xml wrapper that looked like this:
<?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">
</project>
Then I added the specifics of my project like this:
<modelVersion>4.0.0</modelVersion>
<groupId>com.ingeniigroup.stratux</groupId>
<artifactId>AvMet</artifactId>
<version>0.1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
[...]
Once I had done that, maven handled the build just fine.
I wrote a blog entry that has some more background if you need it: http://blog.daviddemartini.com/configure-maven-pom-xml-to-build-integrated-executable-jar-fat-jar/
You can run mvn archetype:generate without parameters. Maven will ask about your project and generate the POM.

How do I get Eclipse to honor a pom.xml full of modules

Using Eclipse Neon 1a Release 4.6.1, Eclipse m2e Version 1.7.0.20160603
Top level pom looks like this:
<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>BORA-WFM</groupId>
<artifactId>BORA-WFM-aggregator</artifactId>
<version>1.0.1</version>
<packaging>pom</packaging>
<modules>
<module>Mike</module>
<module>ike</module>
<module>bike</module>
</modules>
</project>
I've run Project Context Menu Maven => Update Project... but Eclipse / maven refuses to recognize and compile the modules.
How do I make this work?
Eclipse is unable to honor them as IntelliJ can. You can create like a bunch of new (dependent with eachother) projects that are actually Your submodules.
There are two ways:
1.File --> Import --> General/Existing Projects into Workspace
Then You select a path being Your root project, and eclipse will detect subprojects, You can checkbox them and they will be imported
2.You can right click on a project and do Configure-->Detect Nested Projects

Module project is not declared as war package, but get error 'web.xml is missing and <failOnMissingWebXml> is set to true' in eclipse

Module project was declared as war package before in pom.xml, then I removed row <packaging>war</packaging>, I can get expected XX.jar successfully when I run command line mvn install, however, I'm still get error web.xml is missing and <failOnMissingWebXml> is set to true in eclipse.
I have applied 'Force update of snapshot/releases' several times but it's meaningless.
pom.xml
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xx</groupId>
<artifactId>xxx</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>xxx</artifactId>
<name>xxxx</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>xxxx</groupId>
<artifactId>xxxx</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
I found there is web service descriptor however there shouldn't be. I didn't find any way I can delete the descriptor I copy all source files to another folder, create another workspace in eclipse and import the project. All works well.
I had a similar problem - maven project that due to some refactoring went from war package to jar. Was surprisingly difficult to clear the dang marker "web.xml is missing but failOnMissingWebXml is true".
I checked thru the .classpath and .project files - the project in particular had several WST natures that I deleted. After that, I finally deleted the project from Eclipse but left the files on disk, then re-imported the existing maven project into Eclipse. This worked, I think the delete cleared all the hidden metadata.
Just had the same issue - you need to generate a web.xml file.
Right click on Deployment Descriptor in Project Explorer.
Select Generate Deployment Descriptor Stub.
It will generate WEB_INF folder in src/main/webapp and an web.xml in it.
Thanks

Including my own jar in a maven project. What groupId should I provide?

I'm new to using maven and am trying to include my own jar files in a maven project. This is described in the following link - https://maven.apache.org/general.html. In order to understand what the different fields meant in -
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
I took a look at this other link - http://maven.apache.org/guides/mini/guide-naming-conventions.html
Most of the fields are clear to me except the groupId. It says I have to own a domain? Can't I just include my jar in the project without owning a domain?
Here is the pom generated after I use the install command -
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>sorting</groupId>
<artifactId>Algorithms2</artifactId>
<version>1.0</version>
<description>POM was created from install:install-file</description>
</project>
When you create java files, you should have a package name also. It is probably a good idea to use some prefix substring of that package name as a guideline for the groupId.

Categories

Resources