Maven: How to compile only? Keep file structure intact - java

I have a regular java servlet web application with the following structure:
/project/
main_sources/
web/
(java source code files here)
pages/
(jsp files here)
WEB-INF/
web.xml
classes/
(this is where all the compiled java files from main_sources go)
css/
scripts/
This all compiles fine and runs in Tomcat.
I want to compile this project in maven (keep the file structure intact).
What do I put in the POM.xml to NOT produce a WAR/JAR file? I just want it to compile the java source code into the classes folder only.
(Basically, I want to deploy it to Azure App Service and have Maven as a build pipeline task to build the project...but currently it's produce a WAR file instead).

If you call mvn compile, Maven only executes the phases up to the compile phase, so the results will not be packaged.
As a side node: Think about refactoring your project into the standard Maven structure.

You might be doing clean and Build project..Build generates war file.
So ,you select only compile option in maven
To compile project from maven commandline, you can use mvn compile command

Related

How to generate the javascript files in the war directory of GWT application using Maven?

I'm a beginner in Maven and GWT. I am working on the task which is given to me. So, there is a GWT Web Application Project just like Paint and it is developed in Eclipse. It comprises of 3 projects - A, B, and C. The project A is the main project and it is dependent on project B and C. The first thing I did was to convert all of these three projects into Maven projects in Eclipse and under the pom.xml file of the project A, I added the dependencies of the projects B and C along with several plugins and dependencies for running the GWT application in devmode.
After that, I run the following commands in the CMD after navigating to the base directory of the project: mvn war:exploded and mvn gwt:devmode. The first command created an exploded app in specified directory and I was expecting the second command to run the application in the devmode but it didn't happen. Sometimes, the second command showed some kind of errors like not finding the module.gwt.xml file and also like not able to execute the goal. At another instance, the devmode in the browser was launched but it was not showing the web application homepage but was showing the directory of the module.
I just saw the nocache.js file in the war directory but what I wanted was to generate the several javascript cache files in the war folder of my main project. How can I achieve that?
To generate GWT javascript output using maven plugin you should use:
mvn package
The generated JS code should be available in the target folder both as a folder and as a war (it uses the mvn final module name, default to {artifact}-{version}). If your project depends on eachoder, you can use mvn install instead, starting from parent and installing each child progressively.
If you do not see the generated JS files in the output it is bc you have not configured the gwt-maven-plugin properly. Follow the instructions here (https://tbroyer.github.io/gwt-maven-plugin/usage.html). You can start just checking if the maven project type is gwt-app.

multi project java build with gradle

I've decided to use gradle instead of ant in some of my applications.
the folder structure for my apps is:
<appname>
---java
------build.gradle
------settings.gradle
------sourcefolder1
------sourcefolder2
------sourcefolder3
---------------subsourcefolder1
---------------subsourcefolder2
I take the compilation files for every application and create a single jar that contains them all.
My issue is as follows:
There is no code to compile under the java folder, only under its subdirectories which are sub-projects in gradle. So, I apply the java plugin only for the sub-projects. In this case everything compiles fine but the final jar to contain everything doesn't get created. On the other hand, if I apply the java plugin to the rootProject it starts compiling the source folders although they have a uniqe project of their own.
I tried applying the java plugin to the root project and overriding compileJava task to do nothing - but it created an empty jar, probably this task also generates the products to jar.
Is there an elegant solution for this?
Thanks.

How can I organize source-generation in Maven?

I have a Maven plugin which generates sources for my java project and places them into 'target/generated-sources' folder. I need these sources at compile-time. I want to have them in my project while modifying it, already generated. And of course, I want to put them into the correct folder under 'src' folder, and not into 'target/generated-sources'. How can I organize this using Maven?
In other words, I want to be able:
generate sources I need by running some goal of my special source-generating plugin (already done, the sources have the package I specified)
move these generated sources to 'src/main/java/...' folder of standart Maven layout.
remove them from 'target/generated-sources' folder, because otherwise mvn clean install command raises error which says that I have "duplicate class". Indeed, if I just copy generated sources from target to src - I have to classes with the same name and package, though one of them is located in target folder.
Which Maven plugins can hlp with this? I suppose this is a typical task.
The standard solution in maven is to generate all source into target/generated-sources, all source code from target/generated-sources and from src is compiled into target/classes and merged into target jar.
Plugin should never touch files under src directory, because these files are managed by version control system (ex. git).
If plugin is badly written and source files from target/generated-sources is not automatically compilled into target, then use goal build-helper:add-source from Build Helper Maven Plugin as #James Kingsbery said.
In maven-com4j-plugin source code there are comments:
/**
* Directory in which to create the Java COM wrapper files. This directory
* will be added to the Maven project's compile source directory list and
* will therfore be auto-compiled when the Maven compile phase is run.
*
* #parameter expression="${outputDirectory}"
* default-value="${project.build.directory}/generated-sources/com4j/java"
*/
The more important part and solution to your problem is:
This directory
will be added to the Maven project's compile source directory list and
will therfore be auto-compiled when the Maven compile phase is run
So, generated source code should be automatically compiled and archived into builded jar.
You should have a look at the build helper plugin. It allows you to specify additional source directories (such as target/generated-sources). See also Usage of maven Build Helper Maven Plugin.
If your plugin works correctly it will add the generated sources to the internal project and other plugins like the maven-compiler-plugin will pick it up and compile the generatd code.
Within your plugin code you can accomplish this by using something similar:
mavenProject.addCompileSourceRoot( getOutputDirectory().getAbsolutePath() );

Create compile script for Java

I have downloaded an Eclipse project and I want to be able to have other people compile it without using Eclipse. It is a fairly large Java project that is still being worked on. How would I make a compile script that compiles like eclipse?
I highly recommend you look into Maven. Basically you'll define a Maven pom file in the root of your Eclipse project directory which will contain your dependencies (jars) as well as compile and assembly configuration. With this in place you can simply checkout a project and run a maven build command against the local directory you checked out to and an executable/deployable package will be created.

problems deploying a maven application with mvn command

I have a maven application under eclipse . The jsp pages and WEB-INF folder are located under the path NomeMiaApplicazione\src\main\webapp. I run these commands : mvn clean ,
mvn eclipse: eclipse and mvn compile under the root folder of the application, and in all three cases, the build is successful. However when I access the folder ( into the workspace )
. metadata.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\NomeMiaApplicazione
, there are no jsp pages . what would be the solution for this problem ? thks !
It is necessary to clarify the role of the commands you used and their impact on Eclipse.
The mvn eclipse:eclipse command creates the Eclipse project files, in order to save you the hassle of configuring a new project and identifying all the sources for it. This command is also equivalent to creating a new Eclipse project from an existing Maven project using the m2e Eclipse plugin. See also this page regarding the eclipse:eclipse command. That being said, you only need to run such command once, and then import the resulting project into Eclipse.
Secondarily, mvn compile builds your source files into the target directory of your NomeMiaApplicazione root folder. This command does not involve Eclipse in any way. Also, web resources are still not packaged. To package them, you need to issue mvn package: you will then find the <artifact>-<version>.war file again under target, and the pre-packaged content under target\<artifact>-<version>. Beware that, in order to account for the webapp content, your Maven packaging must be of type war. Check the pom.xml for the <packaging> tag.
Finally, deployment is still another issue. If you actually need to move your .war file from the target directory to somewhere else (namely, an autodeploy folder of a servlet container), you can configure the Maven Deploy Plugin and issue mvn deploy. I'd rather suggest you to search SO for deploy war eclipse and/or deploy war maven, since there's plenty of related stuff. In the first case, you will find how to use Eclipse as a facility for deployment, while the second case leverages the command line to provide a more portable/flexible deployment procedure.
That's because mvn compile ends on compile phase of Maven's default lifecycle. Do mvn package and check then. And by the way, default Maven output directory is target so rather check it instead of kind of WTP temporary dirs.

Categories

Resources