Quantcast

How to add class file to build & classpath?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to add class file to build & classpath?

David Hoffer
I'm using exec-maven-plugin to call a main in my code that uses javassist
to generate a class file at build time.  My code places the class file in
the ./target/classes folder so it gets included in the modules normal
binary jar.

However the rest of the build and other code needs to know that the class
exists.  I've added that module/artifact as a dependency but the build and
the runtime classpath has no idea that class exists.

How do I add it to the build and runtime classpath?

-Dave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to add class file to build & classpath?

Curtis Rueden
Hi Dave,

> I'm using exec-maven-plugin to call a main in my code that uses
> javassist to generate a class file at build time.

The more common pattern is to generate .java source files, and then include
them in the build.

Since you are generating .class file(s), could you do it in a separate
module of a multi-module build, then add that module as a dependency to
your main project module?

Could you post an MCVE, particularly your POM, which shows your current
approach?

Regards,
Curtis

--
Curtis Rueden
LOCI software architect - https://loci.wisc.edu/software
ImageJ2 lead, Fiji maintainer - https://imagej.net/User:Rueden


On Thu, Apr 6, 2017 at 10:48 AM, David Hoffer <[hidden email]> wrote:

> I'm using exec-maven-plugin to call a main in my code that uses javassist
> to generate a class file at build time.  My code places the class file in
> the ./target/classes folder so it gets included in the modules normal
> binary jar.
>
> However the rest of the build and other code needs to know that the class
> exists.  I've added that module/artifact as a dependency but the build and
> the runtime classpath has no idea that class exists.
>
> How do I add it to the build and runtime classpath?
>
> -Dave
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to add class file to build & classpath?

David Hoffer
Yes I know its more common to start with java source files but in this case
that is not possible unless someone knows how to generate sources from a
javassist generated CtClass.  If I could do that then yes I could just add
the sources instead of the class.

Here is the best I have so far and sort of works.  Since my class generator
writes to the target/classes folder I told the exec-maven-plugin that and
now it is part of the build as other modules can use the generated class
but its not a great solution as my IDE does not know about the class so it
shows errors.  So apparently IntelliJ's Maven support does not extend this
sort of exec-maven-plugin configuration.  I need to find a better way to do
this so IDE is happy as well.

Here is the relevant part of my pom.

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
            <execution>
                <id>code-generator</id>
                <phase>compile</phase>
                <goals>
                    <goal>java</goal>
                </goals>
                <configuration>

<mainClass>com.issinc.odin.display.metadata.generator.UiDisplayMetadataGenerator</mainClass>

<includeProjectDependencies>true</includeProjectDependencies>
                    <addOutputToClasspath>true</addOutputToClasspath>

<outputDirectory>${project.build.directory}/classes</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

-Dave



On Thu, Apr 6, 2017 at 10:34 AM, Curtis Rueden <[hidden email]> wrote:

> Hi Dave,
>
> > I'm using exec-maven-plugin to call a main in my code that uses
> > javassist to generate a class file at build time.
>
> The more common pattern is to generate .java source files, and then include
> them in the build.
>
> Since you are generating .class file(s), could you do it in a separate
> module of a multi-module build, then add that module as a dependency to
> your main project module?
>
> Could you post an MCVE, particularly your POM, which shows your current
> approach?
>
> Regards,
> Curtis
>
> --
> Curtis Rueden
> LOCI software architect - https://loci.wisc.edu/software
> ImageJ2 lead, Fiji maintainer - https://imagej.net/User:Rueden
>
>
> On Thu, Apr 6, 2017 at 10:48 AM, David Hoffer <[hidden email]> wrote:
>
> > I'm using exec-maven-plugin to call a main in my code that uses javassist
> > to generate a class file at build time.  My code places the class file in
> > the ./target/classes folder so it gets included in the modules normal
> > binary jar.
> >
> > However the rest of the build and other code needs to know that the class
> > exists.  I've added that module/artifact as a dependency but the build
> and
> > the runtime classpath has no idea that class exists.
> >
> > How do I add it to the build and runtime classpath?
> >
> > -Dave
> >
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: How to add class file to build & classpath?

Robert Patrick
In reply to this post by Curtis Rueden
Can't you generated class files in the expected location (in ${project.build.directory}/classes directory) during the compile phase?  I would expect these classes to be included in classpath for later pahses and in the JAR created during the packaging phase of the module build (assuming packaging type is "jar").  Have you tried that?



-----Original Message-----
From: Curtis Rueden [mailto:[hidden email]]
Sent: Thursday, April 06, 2017 11:34 AM
To: Maven Users List
Subject: Re: How to add class file to build & classpath?

Hi Dave,

> I'm using exec-maven-plugin to call a main in my code that uses
> javassist to generate a class file at build time.

The more common pattern is to generate .java source files, and then include them in the build.

Since you are generating .class file(s), could you do it in a separate module of a multi-module build, then add that module as a dependency to your main project module?

Could you post an MCVE, particularly your POM, which shows your current approach?

Regards,
Curtis

--
Curtis Rueden
LOCI software architect - https://urldefense.proofpoint.com/v2/url?u=https-3A__loci.wisc.edu_software&d=DwIBaQ&c=RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=sAgPJflD39NrfRATxD1qVSvbRHcZlzbPxQqHpteGc0M&s=yDl8PjVnueV1WAKs6W36AGN5yriHr-5eTXTPZi_k0kk&e=
ImageJ2 lead, Fiji maintainer - https://urldefense.proofpoint.com/v2/url?u=https-3A__imagej.net_User-3ARueden&d=DwIBaQ&c=RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=sAgPJflD39NrfRATxD1qVSvbRHcZlzbPxQqHpteGc0M&s=biFYk60WuoVvD5FQonUNvMwQmBxfAOcXn6XyvvRRVDw&e= 


On Thu, Apr 6, 2017 at 10:48 AM, David Hoffer <[hidden email]> wrote:

> I'm using exec-maven-plugin to call a main in my code that uses javassist
> to generate a class file at build time.  My code places the class file in
> the ./target/classes folder so it gets included in the modules normal
> binary jar.
>
> However the rest of the build and other code needs to know that the class
> exists.  I've added that module/artifact as a dependency but the build and
> the runtime classpath has no idea that class exists.
>
> How do I add it to the build and runtime classpath?
>
> -Dave
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to add class file to build & classpath?

David Hoffer
Yes that is the location the class file(s) are being created in.  And yes I
am binding the exec-maven-plugin to the compile phase in my pom (see prior
email).  Also the class(s) file is being included in the resulting jar.
However all this is not sufficient for Maven to notice the class(s) file
was generated and add to the build and classpath.

I think part of the reason is that in multi-module maven builds it does not
use the jars for the classpath but rather the pre-jar content (not 100%
certain of that).  However I don't know why Maven doesn't add/use all files
in the ./target/classes folder but only uses files that it put there itself.

What am I missing here?  It seems this should just work because the files
are in the ./target/classes folder.

-Dave

On Thu, Apr 6, 2017 at 10:55 AM, Robert Patrick <[hidden email]>
wrote:

> Can't you generated class files in the expected location (in
> ${project.build.directory}/classes directory) during the compile phase?
> I would expect these classes to be included in classpath for later pahses
> and in the JAR created during the packaging phase of the module build
> (assuming packaging type is "jar").  Have you tried that?
>
>
>
> -----Original Message-----
> From: Curtis Rueden [mailto:[hidden email]]
> Sent: Thursday, April 06, 2017 11:34 AM
> To: Maven Users List
> Subject: Re: How to add class file to build & classpath?
>
> Hi Dave,
>
> > I'm using exec-maven-plugin to call a main in my code that uses
> > javassist to generate a class file at build time.
>
> The more common pattern is to generate .java source files, and then
> include them in the build.
>
> Since you are generating .class file(s), could you do it in a separate
> module of a multi-module build, then add that module as a dependency to
> your main project module?
>
> Could you post an MCVE, particularly your POM, which shows your current
> approach?
>
> Regards,
> Curtis
>
> --
> Curtis Rueden
> LOCI software architect - https://urldefense.proofpoint.
> com/v2/url?u=https-3A__loci.wisc.edu_software&d=DwIBaQ&c=
> RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=
> Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=
> sAgPJflD39NrfRATxD1qVSvbRHcZlzbPxQqHpteGc0M&s=
> yDl8PjVnueV1WAKs6W36AGN5yriHr-5eTXTPZi_k0kk&e=
> ImageJ2 lead, Fiji maintainer - https://urldefense.proofpoint.
> com/v2/url?u=https-3A__imagej.net_User-3ARueden&d=DwIBaQ&c=
> RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=
> Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=
> sAgPJflD39NrfRATxD1qVSvbRHcZlzbPxQqHpteGc0M&s=
> biFYk60WuoVvD5FQonUNvMwQmBxfAOcXn6XyvvRRVDw&e=
>
>
> On Thu, Apr 6, 2017 at 10:48 AM, David Hoffer <[hidden email]> wrote:
>
> > I'm using exec-maven-plugin to call a main in my code that uses javassist
> > to generate a class file at build time.  My code places the class file in
> > the ./target/classes folder so it gets included in the modules normal
> > binary jar.
> >
> > However the rest of the build and other code needs to know that the class
> > exists.  I've added that module/artifact as a dependency but the build
> and
> > the runtime classpath has no idea that class exists.
> >
> > How do I add it to the build and runtime classpath?
> >
> > -Dave
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to add class file to build & classpath?

Russell Gold-3
In reply to this post by David Hoffer
Hi David,

Is this class generated from data files rather than compiled classes? If so, can you bind the plugin run with the generate-resources phase? That would make it available at compile time.


> On Apr 6, 2017, at 11:48 AM, David Hoffer <[hidden email]> wrote:
>
> I'm using exec-maven-plugin to call a main in my code that uses javassist
> to generate a class file at build time.  My code places the class file in
> the ./target/classes folder so it gets included in the modules normal
> binary jar.
>
> However the rest of the build and other code needs to know that the class
> exists.  I've added that module/artifact as a dependency but the build and
> the runtime classpath has no idea that class exists.
>
> How do I add it to the build and runtime classpath?
>
> -Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to add class file to build & classpath?

Curtis Rueden
In reply to this post by David Hoffer
Hi Dave,

I cooked up an example of the multi-module approach which I hope helps:

https://github.com/ctrueden/adhoc-classes

The adhoc-dependency modules just generates a class file by some arbitrary
means via the exec-maven-plugin. In my case, this is a shell script which
calls javac, but in your case it will be your javassist-based approach of
course.

The main-project module depends on the adhoc-dependency module just fine,
and is able to see the generated class without issue.

Does it work for you?

Regards,
Curtis

--
Curtis Rueden
LOCI software architect - https://loci.wisc.edu/software
ImageJ2 lead, Fiji maintainer - https://imagej.net/User:Rueden


On Thu, Apr 6, 2017 at 12:45 PM, David Hoffer <[hidden email]> wrote:

> Yes that is the location the class file(s) are being created in.  And yes I
> am binding the exec-maven-plugin to the compile phase in my pom (see prior
> email).  Also the class(s) file is being included in the resulting jar.
> However all this is not sufficient for Maven to notice the class(s) file
> was generated and add to the build and classpath.
>
> I think part of the reason is that in multi-module maven builds it does not
> use the jars for the classpath but rather the pre-jar content (not 100%
> certain of that).  However I don't know why Maven doesn't add/use all files
> in the ./target/classes folder but only uses files that it put there
> itself.
>
> What am I missing here?  It seems this should just work because the files
> are in the ./target/classes folder.
>
> -Dave
>
> On Thu, Apr 6, 2017 at 10:55 AM, Robert Patrick <[hidden email]
> >
> wrote:
>
> > Can't you generated class files in the expected location (in
> > ${project.build.directory}/classes directory) during the compile phase?
> > I would expect these classes to be included in classpath for later pahses
> > and in the JAR created during the packaging phase of the module build
> > (assuming packaging type is "jar").  Have you tried that?
> >
> >
> >
> > -----Original Message-----
> > From: Curtis Rueden [mailto:[hidden email]]
> > Sent: Thursday, April 06, 2017 11:34 AM
> > To: Maven Users List
> > Subject: Re: How to add class file to build & classpath?
> >
> > Hi Dave,
> >
> > > I'm using exec-maven-plugin to call a main in my code that uses
> > > javassist to generate a class file at build time.
> >
> > The more common pattern is to generate .java source files, and then
> > include them in the build.
> >
> > Since you are generating .class file(s), could you do it in a separate
> > module of a multi-module build, then add that module as a dependency to
> > your main project module?
> >
> > Could you post an MCVE, particularly your POM, which shows your current
> > approach?
> >
> > Regards,
> > Curtis
> >
> > --
> > Curtis Rueden
> > LOCI software architect - https://urldefense.proofpoint.
> > com/v2/url?u=https-3A__loci.wisc.edu_software&d=DwIBaQ&c=
> > RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=
> > Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=
> > sAgPJflD39NrfRATxD1qVSvbRHcZlzbPxQqHpteGc0M&s=
> > yDl8PjVnueV1WAKs6W36AGN5yriHr-5eTXTPZi_k0kk&e=
> > ImageJ2 lead, Fiji maintainer - https://urldefense.proofpoint.
> > com/v2/url?u=https-3A__imagej.net_User-3ARueden&d=DwIBaQ&c=
> > RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=
> > Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=
> > sAgPJflD39NrfRATxD1qVSvbRHcZlzbPxQqHpteGc0M&s=
> > biFYk60WuoVvD5FQonUNvMwQmBxfAOcXn6XyvvRRVDw&e=
> >
> >
> > On Thu, Apr 6, 2017 at 10:48 AM, David Hoffer <[hidden email]>
> wrote:
> >
> > > I'm using exec-maven-plugin to call a main in my code that uses
> javassist
> > > to generate a class file at build time.  My code places the class file
> in
> > > the ./target/classes folder so it gets included in the modules normal
> > > binary jar.
> > >
> > > However the rest of the build and other code needs to know that the
> class
> > > exists.  I've added that module/artifact as a dependency but the build
> > and
> > > the runtime classpath has no idea that class exists.
> > >
> > > How do I add it to the build and runtime classpath?
> > >
> > > -Dave
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
> >
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: How to add class file to build & classpath?

Robert Patrick
In reply to this post by David Hoffer
I also built a little sample project where I simulated a generated class using the maven-resources-plugin to copy a class into the target/classes directory during the process-resources phase.  My unit test in that module works fine and the other module that consumes that class (via a normal dependency) works fine in a multi-module build.  Maybe the issue you are having can be solved by simply moving the exec-maven-plugin execution to the process-resources phase?



-----Original Message-----
From: David Hoffer [mailto:[hidden email]]
Sent: Thursday, April 06, 2017 12:45 PM
To: Maven Users List
Subject: Re: How to add class file to build & classpath?

Yes that is the location the class file(s) are being created in.  And yes I am binding the exec-maven-plugin to the compile phase in my pom (see prior email).  Also the class(s) file is being included in the resulting jar.
However all this is not sufficient for Maven to notice the class(s) file was generated and add to the build and classpath.

I think part of the reason is that in multi-module maven builds it does not use the jars for the classpath but rather the pre-jar content (not 100% certain of that).  However I don't know why Maven doesn't add/use all files in the ./target/classes folder but only uses files that it put there itself.

What am I missing here?  It seems this should just work because the files are in the ./target/classes folder.

-Dave

On Thu, Apr 6, 2017 at 10:55 AM, Robert Patrick <[hidden email]>
wrote:

> Can't you generated class files in the expected location (in
> ${project.build.directory}/classes directory) during the compile phase?
> I would expect these classes to be included in classpath for later
> pahses and in the JAR created during the packaging phase of the module
> build (assuming packaging type is "jar").  Have you tried that?
>
>
>
> -----Original Message-----
> From: Curtis Rueden [mailto:[hidden email]]
> Sent: Thursday, April 06, 2017 11:34 AM
> To: Maven Users List
> Subject: Re: How to add class file to build & classpath?
>
> Hi Dave,
>
> > I'm using exec-maven-plugin to call a main in my code that uses
> > javassist to generate a class file at build time.
>
> The more common pattern is to generate .java source files, and then
> include them in the build.
>
> Since you are generating .class file(s), could you do it in a separate
> module of a multi-module build, then add that module as a dependency
> to your main project module?
>
> Could you post an MCVE, particularly your POM, which shows your
> current approach?
>
> Regards,
> Curtis
>
> --
> Curtis Rueden
> LOCI software architect - https://urldefense.proofpoint.com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=hpzCbX-p3CruRsEhA_MZO805q6XsrzWSSqkZCm7IfAs&s=K-Fe2PmitKUNWXWy2vB1JvN-Ih5TBC0VwxThPwDenU8&e= .
> com/v2/url?u=https-3A__loci.wisc.edu_software&d=DwIBaQ&c=
> RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=
> Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=
> sAgPJflD39NrfRATxD1qVSvbRHcZlzbPxQqHpteGc0M&s=
> yDl8PjVnueV1WAKs6W36AGN5yriHr-5eTXTPZi_k0kk&e=
> ImageJ2 lead, Fiji maintainer - https://urldefense.proofpoint.com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=hpzCbX-p3CruRsEhA_MZO805q6XsrzWSSqkZCm7IfAs&s=K-Fe2PmitKUNWXWy2vB1JvN-Ih5TBC0VwxThPwDenU8&e= .
> com/v2/url?u=https-3A__imagej.net_User-3ARueden&d=DwIBaQ&c=
> RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=
> Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=
> sAgPJflD39NrfRATxD1qVSvbRHcZlzbPxQqHpteGc0M&s=
> biFYk60WuoVvD5FQonUNvMwQmBxfAOcXn6XyvvRRVDw&e=
>
>
> On Thu, Apr 6, 2017 at 10:48 AM, David Hoffer <[hidden email]> wrote:
>
> > I'm using exec-maven-plugin to call a main in my code that uses
> > javassist to generate a class file at build time.  My code places
> > the class file in the ./target/classes folder so it gets included in
> > the modules normal binary jar.
> >
> > However the rest of the build and other code needs to know that the
> > class exists.  I've added that module/artifact as a dependency but
> > the build
> and
> > the runtime classpath has no idea that class exists.
> >
> > How do I add it to the build and runtime classpath?
> >
> > -Dave
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to add class file to build & classpath?

David Hoffer
I'll try to reply to the last 3 posts...

Unfortunately the code uses compiled classes from other modules.  So I
can't change the phase to prior to compile, if I do that then it doesn't
generate the sources.

I did find a way to generate sources instead of class files as I agree that
is a lot nicer...its not quite as easy as javassist but close enough.

However I could not find any combination of moving phases around/etc to
make this work...as I mentioned above if I changed the exec plugin phase to
be prior to compile then it didn't do anything so there was no generated
sources to compile.  It seems Maven just just does not like subprocesses
that generate more sources using the compiled code.

I will look at the above sample projects to see what I missed.

For now I have changed things so that I put my generated code right in the
src/main/java folder then wrap the exec plugin in a profile so I can
manually run that when needed.  Not ideal but its working.  Will
investigate the other projects.

-Dave

On Thu, Apr 6, 2017 at 1:27 PM, Robert Patrick <[hidden email]>
wrote:

> I also built a little sample project where I simulated a generated class
> using the maven-resources-plugin to copy a class into the target/classes
> directory during the process-resources phase.  My unit test in that module
> works fine and the other module that consumes that class (via a normal
> dependency) works fine in a multi-module build.  Maybe the issue you are
> having can be solved by simply moving the exec-maven-plugin execution to
> the process-resources phase?
>
>
>
> -----Original Message-----
> From: David Hoffer [mailto:[hidden email]]
> Sent: Thursday, April 06, 2017 12:45 PM
> To: Maven Users List
> Subject: Re: How to add class file to build & classpath?
>
> Yes that is the location the class file(s) are being created in.  And yes
> I am binding the exec-maven-plugin to the compile phase in my pom (see
> prior email).  Also the class(s) file is being included in the resulting
> jar.
> However all this is not sufficient for Maven to notice the class(s) file
> was generated and add to the build and classpath.
>
> I think part of the reason is that in multi-module maven builds it does
> not use the jars for the classpath but rather the pre-jar content (not 100%
> certain of that).  However I don't know why Maven doesn't add/use all files
> in the ./target/classes folder but only uses files that it put there itself.
>
> What am I missing here?  It seems this should just work because the files
> are in the ./target/classes folder.
>
> -Dave
>
> On Thu, Apr 6, 2017 at 10:55 AM, Robert Patrick <[hidden email]
> >
> wrote:
>
> > Can't you generated class files in the expected location (in
> > ${project.build.directory}/classes directory) during the compile phase?
> > I would expect these classes to be included in classpath for later
> > pahses and in the JAR created during the packaging phase of the module
> > build (assuming packaging type is "jar").  Have you tried that?
> >
> >
> >
> > -----Original Message-----
> > From: Curtis Rueden [mailto:[hidden email]]
> > Sent: Thursday, April 06, 2017 11:34 AM
> > To: Maven Users List
> > Subject: Re: How to add class file to build & classpath?
> >
> > Hi Dave,
> >
> > > I'm using exec-maven-plugin to call a main in my code that uses
> > > javassist to generate a class file at build time.
> >
> > The more common pattern is to generate .java source files, and then
> > include them in the build.
> >
> > Since you are generating .class file(s), could you do it in a separate
> > module of a multi-module build, then add that module as a dependency
> > to your main project module?
> >
> > Could you post an MCVE, particularly your POM, which shows your
> > current approach?
> >
> > Regards,
> > Curtis
> >
> > --
> > Curtis Rueden
> > LOCI software architect - https://urldefense.proofpoint.
> com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=
> RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=
> Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=hpzCbX-p3CruRsEhA_
> MZO805q6XsrzWSSqkZCm7IfAs&s=K-Fe2PmitKUNWXWy2vB1JvN-Ih5TBC0VwxThPwDenU8&e=
> .
> > com/v2/url?u=https-3A__loci.wisc.edu_software&d=DwIBaQ&c=
> > RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=
> > Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=
> > sAgPJflD39NrfRATxD1qVSvbRHcZlzbPxQqHpteGc0M&s=
> > yDl8PjVnueV1WAKs6W36AGN5yriHr-5eTXTPZi_k0kk&e=
> > ImageJ2 lead, Fiji maintainer - https://urldefense.proofpoint.
> com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=
> RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=
> Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=hpzCbX-p3CruRsEhA_
> MZO805q6XsrzWSSqkZCm7IfAs&s=K-Fe2PmitKUNWXWy2vB1JvN-Ih5TBC0VwxThPwDenU8&e=
> .
> > com/v2/url?u=https-3A__imagej.net_User-3ARueden&d=DwIBaQ&c=
> > RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=
> > Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=
> > sAgPJflD39NrfRATxD1qVSvbRHcZlzbPxQqHpteGc0M&s=
> > biFYk60WuoVvD5FQonUNvMwQmBxfAOcXn6XyvvRRVDw&e=
> >
> >
> > On Thu, Apr 6, 2017 at 10:48 AM, David Hoffer <[hidden email]>
> wrote:
> >
> > > I'm using exec-maven-plugin to call a main in my code that uses
> > > javassist to generate a class file at build time.  My code places
> > > the class file in the ./target/classes folder so it gets included in
> > > the modules normal binary jar.
> > >
> > > However the rest of the build and other code needs to know that the
> > > class exists.  I've added that module/artifact as a dependency but
> > > the build
> > and
> > > the runtime classpath has no idea that class exists.
> > >
> > > How do I add it to the build and runtime classpath?
> > >
> > > -Dave
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: How to add class file to build & classpath?

Robert Patrick
> Unfortunately the code uses compiled classes from other modules.  So I can't change the phase to prior to compile, if I do that then it doesn't generate the sources.

In a multi-module project, Maven processes the modules in order (the exception being when running an aggregate plugin) so as long as you don’t have cyclical dependencies between modules (or between the generated classes and the source files within the same module), this shouldn’t matter.  Notice in my little sample project output (below) how the install plugin runs in each module before the next module in the reactor is processed.  Subsequent modules should explicitly declare their dependencies on previously built modules in the normal way and these dependencies come from the local Maven repository when the Classpath is built.

What am I missing?
Robert

C:\test\maven-test\multi-module-test>mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] multi-module-test
[INFO] generator
[INFO] consumer
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building multi-module-test 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ multi-module-test ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ multi-module-test ---
[INFO] Installing C:\test\maven-test\multi-module-test\pom.xml to C:\Users\RPATR
ICK\.m2\repository\test\multi-module-test\1.0\multi-module-test-1.0.pom
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building generator 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ generator ---
[INFO] Deleting C:\test\maven-test\multi-module-test\generator\target
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ generato
r ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:copy-resources (generate-classes) @ gene
rator ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ generator ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @
generator ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\test\maven-test\multi-module-test\generator\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ generator ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\test\maven-test\multi-module-test\generator\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ generator ---
[INFO] Surefire report directory: C:\test\maven-test\multi-module-test\generator\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running test.generated.MyGeneratedClassTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.064 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ generator ---
[INFO] Building jar: C:\test\maven-test\multi-module-test\generator\target\generator-1.0.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ generator ---
[INFO] Installing C:\test\maven-test\multi-module-test\generator\target\generator-1.0.jar to C:\Users\RPATRICK\.m2\repository\test\generator\1.0\generator-1.0.jar
[INFO] Installing C:\test\maven-test\multi-module-test\generator\pom.xml to C:\Users\RPATRICK\.m2\repository\test\generator\1.0\generator-1.0.pom
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building consumer 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ consumer ---
[INFO] Deleting C:\test\maven-test\multi-module-test\consumer\target
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ consumer
 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\test\maven-test\multi-module-test\
consumer\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ consumer ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\test\maven-test\multi-module-test\consumer\
target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @
consumer ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\test\maven-test\multi-module-test\
consumer\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ consume
r ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ consumer ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ consumer ---
[INFO] Building jar: C:\test\maven-test\multi-module-test\consumer\target\consum
er-1.0.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ consumer ---
[INFO] Installing C:\test\maven-test\multi-module-test\consumer\target\consumer-
1.0.jar to C:\Users\RPATRICK\.m2\repository\test\consumer\1.0\consumer-1.0.jar
[INFO] Installing C:\test\maven-test\multi-module-test\consumer\pom.xml to C:\Us
ers\RPATRICK\.m2\repository\test\consumer\1.0\consumer-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] multi-module-test .................................. SUCCESS [  0.524 s]
[INFO] generator .......................................... SUCCESS [  3.373 s]
[INFO] consumer ........................................... SUCCESS [  0.245 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.864 s
[INFO] Finished at: 2017-04-06T16:07:09-05:00
[INFO] Final Memory: 19M/373M
[INFO] ------------------------------------------------------------------------

C:\test\maven-test\multi-module-test>


-----Original Message-----
From: David Hoffer [mailto:[hidden email]]
Sent: Thursday, April 06, 2017 3:26 PM
To: Maven Users List
Subject: Re: How to add class file to build & classpath?

I'll try to reply to the last 3 posts...

Unfortunately the code uses compiled classes from other modules.  So I can't change the phase to prior to compile, if I do that then it doesn't generate the sources.

I did find a way to generate sources instead of class files as I agree that is a lot nicer...its not quite as easy as javassist but close enough.

However I could not find any combination of moving phases around/etc to make this work...as I mentioned above if I changed the exec plugin phase to be prior to compile then it didn't do anything so there was no generated sources to compile.  It seems Maven just just does not like subprocesses that generate more sources using the compiled code.

I will look at the above sample projects to see what I missed.

For now I have changed things so that I put my generated code right in the src/main/java folder then wrap the exec plugin in a profile so I can manually run that when needed.  Not ideal but its working.  Will investigate the other projects.

-Dave

On Thu, Apr 6, 2017 at 1:27 PM, Robert Patrick <[hidden email]>
wrote:

> I also built a little sample project where I simulated a generated
> class using the maven-resources-plugin to copy a class into the
> target/classes directory during the process-resources phase.  My unit
> test in that module works fine and the other module that consumes that
> class (via a normal
> dependency) works fine in a multi-module build.  Maybe the issue you
> are having can be solved by simply moving the exec-maven-plugin
> execution to the process-resources phase?
>
>
>
> -----Original Message-----
> From: David Hoffer [mailto:[hidden email]]
> Sent: Thursday, April 06, 2017 12:45 PM
> To: Maven Users List
> Subject: Re: How to add class file to build & classpath?
>
> Yes that is the location the class file(s) are being created in.  And
> yes I am binding the exec-maven-plugin to the compile phase in my pom
> (see prior email).  Also the class(s) file is being included in the
> resulting jar.
> However all this is not sufficient for Maven to notice the class(s)
> file was generated and add to the build and classpath.
>
> I think part of the reason is that in multi-module maven builds it
> does not use the jars for the classpath but rather the pre-jar content
> (not 100% certain of that).  However I don't know why Maven doesn't
> add/use all files in the ./target/classes folder but only uses files that it put there itself.
>
> What am I missing here?  It seems this should just work because the
> files are in the ./target/classes folder.
>
> -Dave
>
> On Thu, Apr 6, 2017 at 10:55 AM, Robert Patrick
> <[hidden email]
> >
> wrote:
>
> > Can't you generated class files in the expected location (in
> > ${project.build.directory}/classes directory) during the compile phase?
> > I would expect these classes to be included in classpath for later
> > pahses and in the JAR created during the packaging phase of the
> > module build (assuming packaging type is "jar").  Have you tried that?
> >
> >
> >
> > -----Original Message-----
> > From: Curtis Rueden [mailto:[hidden email]]
> > Sent: Thursday, April 06, 2017 11:34 AM
> > To: Maven Users List
> > Subject: Re: How to add class file to build & classpath?
> >
> > Hi Dave,
> >
> > > I'm using exec-maven-plugin to call a main in my code that uses
> > > javassist to generate a class file at build time.
> >
> > The more common pattern is to generate .java source files, and then
> > include them in the build.
> >
> > Since you are generating .class file(s), could you do it in a
> > separate module of a multi-module build, then add that module as a
> > dependency to your main project module?
> >
> > Could you post an MCVE, particularly your POM, which shows your
> > current approach?
> >
> > Regards,
> > Curtis
> >
> > --
> > Curtis Rueden
> > LOCI software architect - https://urldefense.proofpoint.com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=yFgC5rl4Txx-y-pDctEwSxUucsZVoSYiHW3AC93Ql6k&s=_cXs3UmPHprskbNRYYMKDfAuK68WbROM2KLvpH6YnwU&e= .
> com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=
> RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=
> Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=hpzCbX-p3CruRsEhA_
> MZO805q6XsrzWSSqkZCm7IfAs&s=K-Fe2PmitKUNWXWy2vB1JvN-Ih5TBC0VwxThPwDenU
> 8&e=
> .
> > com/v2/url?u=https-3A__loci.wisc.edu_software&d=DwIBaQ&c=
> > RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=
> > Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=
> > sAgPJflD39NrfRATxD1qVSvbRHcZlzbPxQqHpteGc0M&s=
> > yDl8PjVnueV1WAKs6W36AGN5yriHr-5eTXTPZi_k0kk&e=
> > ImageJ2 lead, Fiji maintainer - https://urldefense.proofpoint.com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=yFgC5rl4Txx-y-pDctEwSxUucsZVoSYiHW3AC93Ql6k&s=_cXs3UmPHprskbNRYYMKDfAuK68WbROM2KLvpH6YnwU&e= .
> com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=
> RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=
> Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=hpzCbX-p3CruRsEhA_
> MZO805q6XsrzWSSqkZCm7IfAs&s=K-Fe2PmitKUNWXWy2vB1JvN-Ih5TBC0VwxThPwDenU
> 8&e=
> .
> > com/v2/url?u=https-3A__imagej.net_User-3ARueden&d=DwIBaQ&c=
> > RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=
> > Ql5uwmbofQMW0iErugdCnFgO-CBGr_pt_OzwdxJosG0&m=
> > sAgPJflD39NrfRATxD1qVSvbRHcZlzbPxQqHpteGc0M&s=
> > biFYk60WuoVvD5FQonUNvMwQmBxfAOcXn6XyvvRRVDw&e=
> >
> >
> > On Thu, Apr 6, 2017 at 10:48 AM, David Hoffer <[hidden email]>
> wrote:
> >
> > > I'm using exec-maven-plugin to call a main in my code that uses
> > > javassist to generate a class file at build time.  My code places
> > > the class file in the ./target/classes folder so it gets included
> > > in the modules normal binary jar.
> > >
> > > However the rest of the build and other code needs to know that
> > > the class exists.  I've added that module/artifact as a dependency
> > > but the build
> > and
> > > the runtime classpath has no idea that class exists.
> > >
> > > How do I add it to the build and runtime classpath?
> > >
> > > -Dave
> > >
> >
> > --------------------------------------------------------------------
> > - To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Loading...