What is the best way to add project in Maven Central repository if it uses jar-file and web-resources (js, css, images)?
I can't find any good way to do it so users could add dependency in pom.xml and get all required files (not only jar-file).
I found WebJars project, but it looks a little compicated and requires additional steps (adding and configuring webjars maven plugin before getting my project).
It would be useful to have another project with similar structure.
Thanks for your help!
OK - if you want to distrubute a webapp as a complete package the most common thing to do is package your application as a WAR.
To do this you just need to add the following to your pom:
<packaging>war</packaging>
Don't forget to ensure that your project conforms to the layout specified by the Maven WAR Plugin.
Once you have your WAR deployed, other WAR projects can depend on it which produce a single WAR that is a combination of the two.
If you need something a little more complicated look at using the Maven Assembly Plugin that allows you to create custom archives. As with the WAR, other assembly projects can depend on it, though as the unpacking is in your hands there is again an increase level of complexity.
Related
I have just started working with Maven in Eclipse.
I tried running a sample program in it, and I encountered some problems. The errors were in my pom.xml file. What is the significance of the pom.xml file in Maven?
In short the pom.xml will have all information to build your project.
For example you want to build a project and you have only the pom.xml sent to you via mail. If there are enough entries in the pom.xml then that is all you need! You can import it to Eclipse, Maven will download your source code from CVS, download various dependency jars (like Spring, Apache Commons), run your test cases, build the jar/war, deploy to your jboss/app server, generate a report of your code quality (using Sonar, maybe). Each task you want to do will be mentioned as a goal.
The links already provided are good enough for reference.
POM is an XML file that contains the project configuration details used by Maven. It provides all the configurations required for a project.
POM means Project Object Model, and, as the name suggests, it defines the model of the project as well.
In the normal project development you will add JAR files and libraries as required. In Maven-based development, those JAR files, libraries are added to the project using this pom.xml. In the pom context we call those JAR files, libraries as dependencies.
Maven is a build tool and pom.xml is the main file for the project.
The POM
The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of the information required to build a project in just the way you want. The POM is huge and can be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it effectively.
For more reference, check Maven in 5 Minutes.
POM stands for project object model. It's the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used to build the project. It downloads required libraries easily using POM XML tags.
When there is no Maven, it needs to add all the library JAR files one by one to the project. But when there is Pom.xml there is no need to add library JAR files one by one.
Simply add the dependency to the Pom.xml, and it will automatically add the library JAR files to the project.
pom.xml is a file which describes the project, configures plugins, and declares dependencies. The POM names the project, provides a set of unique identifiers (called coordinates) for a project, and defines the relationships between this project and others through dependencies, parents, and prerequisites.
A POM file can include a modules section, which tells Maven which directories have POM files which need to be built.
In the build section you can define plugins for which you need to build the artifacts in your project.
Pom.xml is part of your maven project, using pom.xml, maven life cycle you can achieve it.
The pom.xml is a project object model which tells everything 3rd party tool dependencies and library's and required plugins it will give everything to your project like project means any java based web content like itself. once you create the maven project you will get the pom.xml blog which is everything to handle your project
If you want to test your project, you need to add testing dependencies which is the maven community it will provide you once added it will have everything tested.
currently we are using ANT script to build the project and running jUnit tests. Now, we decided to move to Maven.
We have two web projects, Core-Project and Sub-Project. Now here it gets complicate. Their project is as follow.
Now, If I want to add this Sub-Project to Core-Project then I will create a jar of Sub-Project with WebContent folder and put it into Core-Project. Now whenever I run Core-Project, we have one utility class which extract content of Sub-Project into Core-Project.
Final(expected) project structure should look like this.
How can I achieve this in Maven? I mean how do I create a jar which contain some files located in src->main->webapp.
It seems that you need to rethink your architecture a bit. This answer may go beyond the scope of your question, but it's important to treat Maven as 'convention over configuration'. It is possible to achieve your layout using a combination of maven packaging tools, but if you restructure and follow Maven conventions, it will make more sense to people outside your project and be less work to maintain.
Suggested:
config-project
sub-project
core-project
config-project can hold the configuration for all parts of your application. sub-project and core-project can depend on this project and use it at runtime. You should package this project as 'jar' or 'zip' depending on the resources you need to make available to other projects.
sub-project should only contain the binary code common to the non-web based part of your application. It should be packaged as a 'jar' project and not be packaged with the config-project dependencies.
core-project should be packaged as a 'war' project. and follow the directory structure as suggested here: Maven War Plugin
Keeping the separation between your configuration, your non-web code and your web code will take a little bit to get used to. There is an excellent archetype by Tomcat which generates a maven project structure composed of these parts. It is easy to generate and inspect: Maven Tomcat Archetype
How can i combine two maven projects. One is webapp, and other is some javascript library (also webapp) which i want to combine with others project.
Or, would be better, how to add some outside folder with js files to maven project that can be deployed on testing server and then build to war.
Have a look at overlays in the Maven WAR Plugin documentation. This explains how Maven merges resources from different web projects into a single WAR.
In a nutshell, you create several WAR files of all the dependencies (usually, you already have this but you can even do this if they aren't real working web projects). Then you can pull these in as dependencies. The important part here is to specify the type of the dependency (<type>war</type>); otherwise Maven will try to add the JAR.
The WAR plugin will notice the additional WARs in the list of dependencies and merge them.
You can create One parent project and Two modul project. You will have 3 pom.xml files.
modul projects extendens dependencies from parent project. Maven parent pom vs modules pom, Multimodule project
Maven WAR Overlays could solve the problem. If you have two maven web projects, and one of your Web Project depends on the other's you could declare the dependent project as a dependency and do an overlay.
Reference:
http://maven.apache.org/plugins/maven-war-plugin/overlays.html
I'm working on a Java Web-related project. At this moment, I try to combine some frameworks all together (Hibernate ORM, Apache Shiro). Of course, in the nearest future, I might need some add-ons via maven dependencies. Is it possible to pack it all together, thus for an emergency case also to pack maven? Due to policy restrictions, there is no chance to install the necessary development tools to make it work.
Is it possible to import maven into WEB - project as JAR?
It seems like you want to just keep your entire project directory if I'm understanding you correctly.
Are you sure that they cannot install Maven? If so then you'd want to package maven with the project.
Could you give me more details about the environment you'd be using?
You could easily import the jar into the maven web project. Under the modules section the web projects pom.xml just specify the directory if it is in the same project. If not, you'll have to add it as a dependency. You'll need the group and artifact ID of the jar you want to add. You can find some pointers here.
An example for the project directory structure:
<modules>
<module>the web part</module>
<module>the backed part</module>
</modules>
Each different module would be a sub module of a master project.
This should help you with setting up the classpath. Also take a look # this:
http://stackoverflow.com/questions/364114/can-i-add-jars-to-maven-2-build-classpath-without-installing-them
I would like to ask these questions related to pom.xml files in Maven projects.
What is a reason to have multiple pom.xml for all dependency libraries instead of having all dependencies in one single pom.xml?
Where should be these pom.xml files in Maven project placed?
This is an example of pom.xml for Spring framework - http://search.maven.org/remotecontent?filepath=org/springframework/spring-core/3.2.5.RELEASE/spring-core-3.2.5.RELEASE.pom
What is a reason to have multiple pom.xml for all dependency libraries instead of having all dependencies in one single pom.xml?
A maven project can be made of many artifacts. One artifact may be a String manipulation library. Another may be a webapp that uses that String manipulation library.
Here's why you shouldn't put all your dependencies in one pom: Your String manipulation library should not have a reference to the servlets.jar just because an unrelated pom is a webapp. Each artifact should have only what it needs in its classpath.
(You may be interested to learn about the dependencyManagement tag, but it does not directly relate to your question.)
Where should be these pom.xml files in Maven project placed?
As #MariuszS linked to, here's the Standard Directory Layout.
At the top level files descriptive of the project: a pom.xml file (and any properties, maven.xml or build.xml if using Ant). In addition, there are textual documents meant for the user to be able to read immediately on receiving the source: README.txt, LICENSE.txt, etc.
This depends on your project, if project is small and product of this project (artifact) is only one then one pom is enough. But if your project have many artifacts (libraries, WARs, EARs etc) then for every artifact pom is required (generally).
Maven concept is that one project definition (POM) generate only one artifact (there are exceptions). Every project should have separate directory with pom.xml inside and source directories if needed.
One maven project can build two diffrent apps (for example desktop and webapp). This diffrent applications has diffrents dependencies.
Sample multimodule project structure: https://github.com/peter-lawrey/Java-Chronicle
Read more
Introduction to the Standard Directory Layout
Guide to Working with Multiple Modules
Maven by Example
Chapter 6. A Multi-module Project