Maven Antlr3 plugin generates code in weird location - maven

I am using plugin and Antlr version 3.3 for a project under H:/compiler
I have a tokens file under my src/main/antlr3/com/cbc/example directory called CBCTokens.g. In the same package i have a parser grammar file called MyScribe.g that references the tokens using tokenVocab=CBCTokens. I also have a tree grammar in the same directory.
However when i try to execute the build, i get an error on the very first file the plugin encounters saying:
Error(1): cannot write file : java.io.FileNotFoundException: H:\compiler\target\generated-sources\antlr3\H:\compiler\src\main\antlr3\CBCTokensLexer.java (The filname, directory name, or volume label syntax is incorrect)
It seems to me that the plugin is determining the output path using some weird combination of baseDir and the default output directory.
What configuration am i missing?
Thanks

First best thing is never to generate code into src/main/ folder whatever. This means for your configuration just remove <outputDirectory> tag and remove the <sourceDirectory> cause it's the default of the antlr3-maven-plugin.
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr3-maven-plugin</artifactId>
<version>3.3</version>
<configuration>
<printGrammar>false</printGrammar>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Related

How to Read a Property File in a Maven Project?

I've read all the related entries on this topic but nothing is working.
I have placed the database property file under resources/property/db.properties, added the following to Maven:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>${project.build.outputDirectory}/property/db.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
and using the following code to get the stream:
this.getClass().getClassLoader().getResourceAsStream("db.properties");
but when the Properties object loads it, it strangely only contains a single property defined in pom.xml (which is an encoding property)!
I have tried the following as well at no avail:
this.getClass().getClassLoader().getResourceAsStream("property/db.properties");
I can see that the file is at target's class/property/db.properties (or in latter case, in class/db.properties) when the project gets built and deployed (Tomcat). Is there something I am missing here?
UPDATE
As it is suggested in the comment below, use "/" to prefix the path to the property file.
If you want to do it using properties-maven-plugin, make sure property values are indicated in Properties section of POM.xml. This plugin then grabs these values and puts them in a property file indicated by outputFile tag.

Create multiple war files and rename them with Maven

For the first part : I want to generate multiple war files when I build a maven project.
The second part : I want to rename those files dynamically.
For example : the file that is generated now is called test.war . If I want to generate this file multiple times on a single build I would want the files to be name like this : test_1.war, test_2.war , test_3.war...
Do you guys have any Idea how can this be achieved ? I can also create a .bat file that would do those things but I am not quite sure how :)
Thank you
Not entirely sure whether this works but possibly you could try something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>test_1</id>
<goals>
<goal>war</goal>
<goals>
<configuration>
<warName>test_1.war</warName>
</configuration>
</execution>
<execution>
<id>test_2</id>
<goals>
<goal>war</goal>
<goals>
<configuration>
<warName>test_2.war</warName>
</configuration>
</execution>
<executions>
</plugin>
thank you for all your answers. I managed to create those files using a .bat file that looks like this :
call mvn -DfinalName=test_1 package
call mvn -DfinalName=test_2 package
call mvn -DfinalName=test_3 package
call mvn -DfinalName=test_4 package
and in the pom.xml I added a property <finalName> :
<properties>
<finalName>${project.artifactId}-${project.version}</finalName>
</properties>
and in the build section I added this :
<build>
<finalName>${finalName}</finalName>
...
</build>
.

How to compile resources folder with Maven command

I'm using mvn compile to compile my Maven webapp. This project has a resources folder instead of the java folder created for a .jar project. My problem is that mvn finds no sources, and I haven't find a way in the maven docs to proceed this way. Is there a way, either by mvn command options or by pom.xml modification to make mav aware of the resources folder and compile it?
I know changing the name from resources to java makes the deal, but that's a spureous way to proceed.
To include additional source directories in your project you can use the Build Helper Maven Plugin
So for example the following configuration will add the src/main/resources folder of your project as a source folder.
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/main/resource</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Look at your .classpath file. That should have what folders including src and test are added. You can then add additional resources. I would normally use the IDE to look at the build path and add/exclude resources.

What is the format for specifying a package in the Antlr4 maven plugin?

What is the format for specifying a package in the Antlr4 maven plugin antlr4-maven-plugin?
I feel like I should be able to do the following:
<plugin>
<groupId>com.tunnelvisionlabs</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.0</version>
<configuration>
<arguments>package my.package.name</arguments>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
but that results in the following error:
[ERROR] Failed to execute goal com.tunnelvisionlabs:antlr4-maven-plugin:4.0:antlr4 (default) on project my_project: Unable to parse configuration of mojo com.tunnelvisionlabs:antlr4-maven-plugin:4.0:antlr4 for parameter arguments: Cannot assign configuration entry 'arguments' with value 'package my.package.name' of type java.lang.String to property of type java.util.List -> [Help 1]
If I am you, I will make a maven project per package and try this
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.0</version>
<configuration>
<sourceDirectory>${basedir}/src</sourceDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
but usually, When I pass an argument in maven configuration, I do the following. but I am not sure of that syntax in antlr4
<plugin>
<groupId>com.tunnelvisionlabs</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.0</version>
<configuration>
<arguments>
<argument>-package</argument>
<argument>my.package.name</argument>
</arguments>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
Edit: Notice the - in front of package so the antlr-maven-plugin will recognize it as a parameter
The package is automatically determined based on the location of the file in your project, similar to the way the package is determined for Java files. The output is also placed in a location determined by the location of the source file. To change the package where the code is generated, you'll need to move the grammar file.
Other arguments can be specified like this:
<arguments>
<argument>arg1</argument>
<argument>arg2</argument>
</arguments>
Your configuration arguments syntax is wrong.
Please change the configuration of antlr4-maven-plugin from
<configuration>
<arguments>package my.package.name</arguments>
</configuration>
to:
<configuration>
<arguments>
<argument>-package</argument>
<argument>my.package.name</argument>
</arguments>
</configuration>
In order to add package information to the generated code you must add the following annotation to the g4 file:
#header {
package com.this.is.my.package;
}
I tried
#header {
package com.this.is.my.package;
}
but when you have imports it adds package line for each file imported and as a result compiler errors raised in generated file. You have to be careful to add #header so file with imported grammars had only one package line. I think it's a bug.
I have the Demo.g4 inside src/main/antlr4/de/schmitz.
Now the classes are generated to target/generated-sources/antlr4/de/schmitz.
The package is de.schmitz.
Everything is correct.
Now I want to change the package and folders of the generated classes (actually NOT moving my Demo.g4):
<arguments>
<argument>-package</argument>
<argument>my.package</argument>
</arguments>
The classes are generated to target/generated-sources/antlr4/de/schmitz.
The package is my.package.
This cannot be compiled, so it could be an error.
So I want to change the output directory:
<outputDirectory>${project.build.directory}/generated-sources/antlr4/my/package</outputDirectory>
Now it get's buggy.
The package is my.package.
But the folder is target/generated-sources/antlr4/my/package/de/schmitz.
So it's concatenated instead of overwritten.

jaxb2-maven plugin

I wanna generated java classes from xsd files bt soome how whenever i run the code it shows the error
No Schema has been found... here is the code... Kindly help...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>src/main/resources/xsd</schemaDirectory>
<includeSchema>**/*.xsd</includeSchema>
<!-- <generatepackage>org.onesync.esb.datasync.model</generatepackage> -->
<!-- The package in which the source files will be generated. -->
<packageName>org.onesync.esb.datasync.model</packageName>
<!-- The working directory to create the generated java source files. -->
<outputDirectory>src/main/java/org/onesync/esb/datasync/model</outputDirectory>
</configuration>
</plugin>
</plugins>
I don't think <includeSchema>**/*.xsd</includeSchema> is valid syntax for jaxb2-maven-plugin:xjc Try omitting that parameter.
If you don't specify schemaFiles it should use all XSD files in the schemaDirectory.
"schemaFiles -- List of files to use for schemas, comma delimited. If none, then all xsd files are used in the schemaDirectory. This parameter also accepts Ant-style file patterns." (see jaxb2-maven-plugin documentation)
BTW, it is usually a good idea to use maven's configuration parameters to refer to a directory. For example, change <schemaDirectory>src/main/resources/xsd</schemaDirectory> to <schemaDirectory>${project.basedir}/src/main/resources/xsd</schemaDirectory>.
Finally, you might also want to refer to this similar SO post.
I am also using maven configuration and spent almost to compile the project stuff. Later on i came to know that it looking for the schema file i.e. schema.xsd.
If you are using the MAVEN configuration then by default you can put the schema file under the resources directory.
But if you want to specify your path for finding the schema file then you can use the includeSchema tag of schemaDescription in plugin configuration.
OR
You can use the effictive pom to search for specific tag also.
Command for effective pom in maven: mvn help:effective-pom
Thanks

Resources