Eclipse on-click deploy to remote Tomcat - eclipse

I've been looking for this all-over the internet and somehow I can't find a easy way to do it.
What I need is really simple and I believe that many of you probably do it already:
- I develop Java Web Apps in Eclipse and so does my team;
- we have a tomcat7 server running on a Ubuntu machine which works as a centralized Dev environment;
- I would like to click a deploy button and send the new data to the server and deploy it (reload it), instead of exporting a war every time and manually upload it to server.
Up till now seems like the only way to do it is with Maven plugin for eclipse, which uses the manager/HTML interface of tomcat.
Problem: I just can't get it to work. But somehow I can't find a simple walk through that explains how to do it. I'm not too experienced with eclipse or Linux but the configuration of local tomcat servers seems pretty straightforward. I don't understand why is so hard to install a remote one.
Could you please help me out by explaining in detail how to do it? Thank you in advance for you patience.

Yes, you can use Tomcat7 Maven Plugin. Here is the steps:
1) Install Maven Integration for Eclipse (m2eclipse) to your eclipse from Eclipse Marketplace etc.
1.1) Navigate to Help -> Eclipse Marketplace and search "Maven Integration for Eclipse".
2) From eclipse, create a maven project.
2.1) Navigate to File -> New -> Project... -> Maven -> Maven Project.
2.2) Click Next (Leave all fields with default).
2.3) Select "maven-archetype-webapp" and click Next.
2.4) Enter arbitrary value on Group Id and Artifact Id. (e.g. "org.myorg" for Groupd Id and "myapp" for Artifact Id) and click Finish. (You will see pom.xml in your project's root.)
3) Edit pom.xml like this: (Replace yourhost below with your hostname or ip address.)
<project ...>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<url>http://yourhost:8080/manager/text</url>
</configuration>
</plugin>
</plugins>
</build>
</project>
4) Add following lines to your CATALINA_BASE/conf/tomcat-users.xml and restart your tomcat.
<tomcat-users>
...
<role rolename="manager-script"/>
<user username="admin" password="" roles="manager-script"/>
</tomcat-users>
5) From eclipse, run tomcat7:redeploy goal.
5.1) Right click your project and navigate to Run As -> "Maven build...".
5.2) Enter tomcat7:redeploy to Goals and click Run.
6) Once you create the run configuration setting above, you can run tomcat7:redeploy goal from Run -> Run Configurations.
Please refer to the following documents for details:
http://tomcat.apache.org/tomcat-7.0-doc/manager-howto.html#Configuring_Manager_Application_Access
http://tomcat.apache.org/maven-plugin-2.1/index.html
http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/plugin-info.html
If you use another user instead of admin with empty password (which is plug-in's default), you need to create %USERPROFILE%.m2\settings.xml and edit pom.xml like below:
%USERPROFILE%.m2\settings.xml:
<settings>
<servers>
<server>
<id>tomcat7</id>
<username>tomcat</username>
<password>tomcat</password>
</server>
</servers>
</settings>
%USERPROFILE% is your home folder. (e.g. C:\Users\yourusername)
pom.xml:
<configuration>
<server>tomcat7</server>
<url>http://localhost:8080/manager/text</url>
</configuration>
Add server tag.

Related

How can I configure Maven (command line) and the IDE to build in different folders?

I'm often switching between my IDE (running just the unit tests for the code which I'm working on) and the command line (running all unit tests).
This causes problems: Sometimes, mvn clean fails because the IDE is building the code. Or I get weird errors in the IDE about missing classes because the IDE is reading files which Maven has modified.
Also, I can't continue working in the IDE why Maven builds in the background.
How can I configure my IDE or Maven to use different build folders?
Use profiles and this code in your (parent) POM:
<project>
...
<build>
<outputDirectory>${basedir}/${target.dir}/classes</outputDirectory>
<testOutputDirectory>${basedir}/${target.dir}/test-classes</testOutputDirectory>
</build>
<properties>
<target.dir>target</target.dir>
</properties>
<profiles>
<profile>
<id>ide-folders</id>
<properties>
<target.dir>target-ide</target.dir>
</properties>
</profile>
</profiles>
...
Configure the project in your IDE to enable the profile target-ide when building. Now Maven and the IDE use different folders.
Alternatively, you could enable this profile in your settings.xml and make the folder target-ide the default. This way, you wouldn't have to modify the settings of many projects in your IDE. It would even work on your CI build server (it would build into target-ide but who cares? And if you cared enough, you can enable the profile there as well).
Note: This is based on a blog-post on jroller.com which is down. The Internet Archive has a copy: https://web.archive.org/web/20150527111413/http://www.jroller.com/eu/entry/configuring_separate_maven_output_folders

Creating second customized POM in eclipse

Within a maven project under eclipse, I want to have a second(or customized) pom.xml in which I can use plugings like the assembly-plugin.
The problem with this plugin is that it requieres an outputh path which is only interesting for me.
Since I'm using git to push to a remote repository, I don't want to pollute the version controlled pom.xml with my private paths and other stuff.
I read about inheritance and multi-mode possibilities, but I only need two poms:
1) One for the public with general settings
2) One only for me with cusotimzed build options
I tried to create a second pom file and wanted to build the project with a new run configuration, but I don't know how to pass the -f parameter(which should call a different pom) in that dialog.
Thanks for hints or best practices.
Example of what I want to put in the custom pom:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.1</version>
<configuration>
<outputDirectory>some\private\path</outputDirectory>
<finalName>SomeName</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</plugin>
Select the second POM in the Package Explorer, right click -> Run As -> Maven Build...
That should run Maven with the custom POM.
If you don't get the Maven options in the "Run As" menu, go to the "Content Types" preferences page -> Text -> XML -> Maven POM XML.
Add the name of your custom POM so Eclipse understands that this is also a POM (I'm not 100% sure it will look inside a file to determine the type).
If that also fails, you can use a trick: Write a small tool that takes the unmodified POM, adds the XML which you need and then runs Maven. On Linux, you can use shell scripts for that. On Windows, a small Java program might be easier. Or have a look at PowerShell.

Maven profiles are not considered in Eclipse

I have three profiles defined in my pom.xml:
<profiles>
<profile>
<id>ABC</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<url.base>http://server1.de</url.base>
<url.searchevse>/search</url.searchevse>
<url.reservation>/reservation</url.reservation>
<url.cancelation>/reservation/cancel</url.cancelation>
<xxx.devmode>false</xxx.devmode>
</properties>
</profile>
<profile>
<id>XYZ</id>
<properties>
<url.base>http://server2.de</url.base>
<url.searchevse>/cns/search</url.searchevse>
<url.reservation>/cns/reservation</url.reservation>
<url.cancelation>/cns/cancel_reservation</url.cancelation>
<xxx.devmode>false</xxx.devmode>
</properties>
</profile>
<profile>
<id>DEVELOPMENT</id>
<properties>
<url.base>http://localhost/noservices</url.base>
<url.searchevse>/no/search</url.searchevse>
<url.reservation>/no/reservation</url.reservation>
<url.cancelation>/no/cancel_reservation</url.cancelation>
<xxx.devmode>true</xxx.devmode>
</properties>
</profile>
</profiles>
In Eclipse I have a Run Configuration
clean install XYZ
and I tried both using -PXYZ (and -P XYZ) in the Goals field as well as
clean install
in the Goals field and XYZ in the Profiles field.
The problem:
The defined profile is never used.
Inserting the active profile under Properties-->Maven-->Active Maven Profiles doesn't work (or do I have to use a special syntax, e.g. no spaces after a comma or so).
Right click your project in the Project Explorer then go to
Properties --> Maven --> Active Maven Profiles
and type in only the profile name that you want to run.
If you want to run your ABC profile, type in ABC in the Active Maven Profile input box.
The description(separated by commas) given there is a bit confusing.
Once you define your profile name or id in the input box. You can clean and run your project on your server.
By doing so, your mentioned active maven profile will be run.
verify that plugin execution id are not equal in different profiles
For those who still have problems. I am not really used to Eclipse nor STS but I actually use Spring Tool Suite Version: 4.7.1.RELEASE. I think the Active Maven Profile input box. may be named Write here your already activated Maven Profile :) as I think its purpose is to select an activated profile to run.
In my case, what I did was :
edit my Maven settings.xml (maven folder, conf folder) file to activate the desired profile with <activeProfile>MyProfileId</activeProfile> in the activeProfiles section. The one thing I really appreciate is that it accepts profile even defined in your project pom.xml and that is great for peoject with sub modules. If you have time, read this https://maven.apache.org/guides/introduction/introduction-to-profiles.html
Then add your profile to Active Maven Profile input box (Ctrl+Alt+P or right click on your project, Maven, Select Maven profiles). You can also deactivate a profile with ! or - before a profile name.
You might need to restart Eclipse.
The first step is only to activate your profile. So if you have any way to achieve this, feel free to do as so.
In Eclipse 4.20 you can right click on project -> Maven -> Select Maven Profiles...

Failed to resolve version for org.apache.maven.archetypes

I have configured maven3.0.3 in my local machine. Have installed m2e eclipse plugin.
But when i try to create a new maven project using maven-archetype-webapp, i get the following exception.
Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:RELEASE from any of the configured repositories.
Could not resolve artifact org.apache.maven.archetypes:maven-archetype- webapp:pom:RELEASE
Failed to resolve version for org.apache.maven.archetypes:maven-archetype- webapp:pom:RELEASE: Could not find metadata org.apache.maven.archetypes:maven-archetype- webapp/maven-metadata.xml in local ([HOME]/.m2/repository)
Failed to resolve version for org.apache.maven.archetypes:maven-archetype- webapp:pom:RELEASE: Could not find metadata org.apache.maven.archetypes:maven-archetype- webapp/maven-metadata.xml in local ([HOME]/.m2/repository)
I do some processing behind a proxy and the proxy configurations are updated in {HOME}/.m2/settings.xml and M2_HOME/conf/settings.xml.
The archetype generate command works fine in command line. It downloaded the dependencies through proxy.
Any help is greatly appreciated.
Edit 05-10-2012
While creating a new Maven Web project in eclipse, the archetype "maven-archetype-webapp" version is displayed as RELEASE. Is this in anyway linked?
I had the same problem. I fixed it by adding the maven archetype catalog to eclipse. Steps are provided below:
Open Window > Preferences
Open Maven > Archetypes
Click 'Add Remote Catalog' and add the following:
Catalog File: https://repo1.maven.org/maven2/archetype-catalog.xml
Description: maven catalog
I found the following tutorial very useful.
Step1: The maven command used to create the web app:
mvn archetype:generate -DgroupId=test.aasweb -DartifactId=TestWebApp -DarchetypeArtifactId=maven-archetype-webapp
Step2: The following entry was added onto the project's pom.xml.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<wtpapplicationxml>true</wtpapplicationxml>
<wtpversion>1.5</wtpversion>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<classpathContainers>
<classpathContainer>
org.eclipse.jst.j2ee.internal.web.container
</classpathContainer>
<classpathContainer>
org.eclipse.jst.j2ee.internal.module.container
</classpathContainer>
/classpathContainers>
<additionalProjectFacets>
<jst.web>2.5</jst.web>
<jst.jsf>1.2</jst.jsf>
</additionalProjectFacets>
</configuration>
</plugin>
Step3: Run the maven command to convert into eclipse project format.
mvn eclipse:clean eclipse:eclipse
Step4: Import the project onto eclipse as Existing Maven project.
If you are using eclipse, you can follow the steps here (maven in 5 min not working) for getting your proxy information. Once done follow the steps below:
Go to Maven installation folder C:\apache-maven-3.1.0\conf\
Copy settings.xml to C:\Users\[UserFolder]\.m2
Modify the proxy in settings.xml based on the info that you get from the above link.
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>your proxy</host>
<port>your port</port>
</proxy>
Open eclipse
Go to: Windows > Preferences > Maven > User Settings
Browse the settings.xml from .m2 folder
Click Update Settings
Click Reindex
Apply the changes and Click OK
You can now try to create Maven Project in Eclipse
Read carefully about the reason.
"Failed to resolve version for org.apache.maven.archetypes:maven-archetype- webapp:pom:RELEASE: Could not find metadata org.apache.maven.archetypes:maven-archetype- webapp/maven-metadata.xml in local"
So all you need to do is download the maven-metadata.xml
to your {HOME}.m2\repository
That's it.
You do need to have a settings.xml linked under user settings (Located in preferences under maven)
But, if that doesn't fix it, like it didn't for many of you. You also have to delete the directory:
.m2/repository/org/apache/maven/archetypes/maven-archetype-quickstart
then quit eclipse and try again.
This is what solved my problem.
Go to Windows-> Preference-> Maven -> User settings
Select settings.xml of Maven
Restart Eclipse
I had a similar problem building from just command line Maven. I eventually got past that error by adding -U to the maven arguments.
Depending on how you have your source repository configuration set up in your settings.xml, sometimes Maven fails to download a particular artifact, so it assumes that the artifact can't be downloaded, even if you change some settings that would give Maven visibility to the artifact if it just tried. -U forces Maven to look again.
Now you need to make sure that the artifact Maven is looking for is in at least one of the repositories that is referenced by your settings.xml. To know for sure, run
mvn help:effective-settings
from the directory of the module you are trying to build. That should give you, among other things, a complete list of the repositories you Maven is using to look for the artifact.
I too had same problem but after searching solved this. go to menu --> window-->preferences-->maven-->Installations-->add--> in place of installation home add path to the directory in which you installed maven-->finish-->check the box of newly added content-->apply-->ok. now create new maven project but remember try with different group id and artifact id.
The right way to solve my problem are as followed. Hope to be helpful to others.
the errors informations.
Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:1.0 from any of the configured repositories. Could not resolve artifact org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0 Failure to transfer org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0
Delete the maven-archetype-webapp:1.0 in the directory ~/.m2/repository/org/Apache/maven/archetypes
Download the maven-archetype-webapp:1.0 and the maven-archetype-webapp-1.0.pom from
http://maven.ibiblio.org/maven2/org/apache/maven/archetypes/maven-archetype-webapp/1.0/
execute mvn install:install-file -DgroupId=org.apache.maven.archetypes -DartifactId=maven-archetype-quickstart -Dversion=1.1 -Dpackaging=jar -Dfile=此处填maven-archetype-webapp-1.0的路径.
try to establish a maven project of webapp to test whether the problem has solved.
Create New User Environment Variables:
MAVEN_HOME=D:\apache-maven-3.5.3
MAVEN=D:\apache-maven-3.5.3\bin
MAVEN_OPTS=-Xms256m -Xmx512m
Appened below in Path variable (System Variable):
;D:\apache-maven-3.5.3\bin;
This worked for me:- navigate to windows-> preferences-> maven
and check the "download artifacts sources" and click apply.
No need to do all above lengthy steps.
Simply delete c:\Users\.m2\Repository\org folder
Maven will automatically downloads what it needs
I simple use below steps:
Create Maven project -> check checkobox -> "Create a simple project (skip archetype selection)"
It works for me
I downloaded the jar and pom from:
here
and put them here:
\.m2\repository\org\apache\maven\archetypes\maven-archetype-quickstart\1.1\
I also got same error ....And i found that my internet connection is closed so eclipse is unable to download repositories for webapps archetypes and when i got internet connection i just created again maven project with webapps archetypes and my eclipse downloaded repositories and its done...
This is 100% working:
Delete the .m2 folder which is present in your Desktop>User
connect your pc to the Internet
Create your maven project again
Thats it, hope it helps
Try , It worked for = I just removed "archetypes" folder from below location
C:\Users\Lenovo.m2\repository\org\apache\maven
But you may change following for experiment - download latest binary zip of Maven, add to you C:\ drive and change following....
Change Proxy
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username></username>
<password></password>
<host>10.23.73.253</host>
<port>8080</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
I had the same problem i solved it by only adding remote catalog
in eclipse go to Window -> Preferences ->Maven ->Archetypes ->click on add remote Catalog then a window will open in that paste
http://repo.maven.apache.org/maven2/archetype-catalog.xml
in that catalog file then hit ok restart eclipse now all working fine
The problem may also come from that you haven't set MAVEN_HOME environment variable. So the Maven embedded in Eclipse can't do its job to download the archetype.Check if that variable is set upfront.

Get source jar files attached to Eclipse for Maven-managed dependencies

I am using Maven (and the Maven Eclipse Integration) to manage the dependencies for my Java projects in Eclipse. The automatic download feature for JAR files from the Maven repositories is a real time saver. Unfortunately, it does not include API documentation and source code.
How can I set up Maven to automatically also get the source and javadoc attachments and register them properly with Eclipse?
I am sure m2eclipse Maven plugin for Eclipse - the other way around - can do that. You can configure it to download both the source files and javadoc automatically for you.
This is achieved by going into Window > Preferences > Maven and checking the "Download Artifact Sources" and "Download Artifact JavaDoc" options.
mvn eclipse:eclipse -DdownloadSources=true
or
mvn eclipse:eclipse -DdownloadJavadocs=true
or you can add both flags, as Spencer K points out.
Additionally, the =true portion is not required, so you can use
mvn eclipse:eclipse -DdownloadSources -DdownloadJavadocs
The other answers on this work, but if you want to avoid having to remember command line arguments, you can also just add to the downloadSources and downloadJavadocs config to the maven-eclipse-plugin section of your pom.xml:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
... other stuff ...
</configuration>
</plugin>
</plugins>
</build>
...
</project>
I prefer not to put source/Javadoc download settings into the project pom.xml file as I feel these are user preferences, not project properties. Instead, I place them in a profile in my settings.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>sources-and-javadocs</id>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>sources-and-javadocs</activeProfile>
</activeProfiles>
</settings>
Right click on project -> maven -> download sources
If the source jars are in the local repository and you are using Eclipses maven support the sources are getting automatically attached. You can run mvn dependency:sources to download all source jars for a given project. Not sure how to do the same with the documentation though.
If you are using meclipse do
window --> maven --> Download Artifact Sources (select check)
(If you still get attach source window, then click on attach file button and close the attach source window. The next time you try to see the source it will open the correct source)
There is also a similiar question that answers this and includes example pom settings.
I tried windows->pref..->Maven But it was not working out. Hence I created a new class path with command mvn eclipse:eclipse -DdownloadSources=true and refreshed the workspace once. voila.. Sources were attached.
Source jar's entry is available in class path. Hence new build solved the problem...
in my version of Eclipse helios with m2Eclipse there is no
window --> maven --> Download Artifact Sources (select check)
Under window is only "new window", "new editor" "open perspective" etc.
If you right click on your project, then chose maven--> download sources
Nothing happens. no sources get downloaded, no pom files get updated, no window pops up asking which sources.
Doing mvn xxx outside of eclipse is dangerous - some commands dont work with m2ecilpse - I did that once and lost the entire project, had to reinstall eclipse and start from scratch.
Im still looking for a way to get ecilpse and maven to find and use the source of external jars like servlet-api.
Changing pom for maven-eclipse-plugin to include source/javadoc just apply for new dependencies being added to pom. If we need to apply for existing dependencies, we must run mvn dependency:sources. I checked this.
Checking download source/javadoc in Eclipse-Maven preference, sometimes is not enough. In the event maven failed to download them for some reason (a network blackout?), maven creates some *.lastUpdated files, then will never download again. My empirical solution was to delete the artifact directory from .m2/repository, and restart the eclipse workspace with download source/javadoc checked and update projects at startup checked as well.
After the workspace has been restarted, maybe some projects can be marked in error, while eclipse progress is downloading, then any error will be cleared.
Maybe this procedure is not so "scientific", but for me did succeded.
I've added the pom configuration to the maven-eclipse plugin to download source and javadocs, but I figure/hope that will happen for new dependencies, not existing ones.
For existing dependencies, I browsed in package explorer down to the "Maven Dependencies" and right-clicked on commons-lang-2.5.jar, selected Maven | Download Sources and... nothing appeared to happen (no progress bar or indication that it was doing anything). It did, however, download as I'm able to jump to source in commons-lang now.
overthink suggested using the setup in the pom:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
... other stuff ...
</configuration>
</plugin>
</plgins>
</build>
...
First i thought this still won't attach the javadoc and sources (as i tried unsuccessfully with that -DdownloadSources option before).
But surprise - the .classpath file IS getting its sources and javadoc attached when using the POM variant!
For Indigo (and probably Helios) the checkboxes mentioned above are located here:
Window -> Preferences -> Maven
I had a similar problem, and the solution that worked best for me was to include the source in the same jar as the compiled code (so a given directory in the jar would include both Foo.java and Foo.class). Eclipse automatically associates the source with the compiled code, and automatically provides the JavaDoc from the source. Obviously, that's only helpful if you control the artifact.
After Setting the Properties either at Project Level or User Properties level,
Please do a Maven -> Update Project (Force Update). It downloads the sources
A Small addition to the answer, if your project is not a maven project still you can get the source code of the jars, by using this plugin provided in eclipse
Java Source Attacher