@@ -593,34 +593,67 @@ func (t *TomcatContainer) Finalize() error {
593593 return fmt .Errorf ("failed to create context directory: %w" , err )
594594 }
595595
596- appContextXML := filepath .Join (buildDir , "META-INF" , "context.xml" )
597- var contextContent string
598-
599- if _ , err := os .Stat (appContextXML ); err == nil {
600- xmlBytes , err := os .ReadFile (appContextXML )
601- if err != nil {
602- return fmt .Errorf ("failed to read META-INF/context.xml: %w" , err )
596+ if _ , statErr := os .Stat (contextXMLPath ); os .IsNotExist (statErr ) {
597+ appContextXML := filepath .Join (buildDir , "META-INF" , "context.xml" )
598+ var contextContent string
599+
600+ if _ , err := os .Stat (appContextXML ); err == nil {
601+ xmlBytes , err := os .ReadFile (appContextXML )
602+ if err != nil {
603+ return fmt .Errorf ("failed to read META-INF/context.xml: %w" , err )
604+ }
605+
606+ xmlStr := string (xmlBytes )
607+ xmlStr = strings .TrimSpace (xmlStr )
608+
609+ contextContent = injectDocBase (xmlStr , "${user.home}/app" )
610+ t .context .Log .Info ("Merged META-INF/context.xml with %s - realm and resource configurations preserved" , contextXMLName )
611+ } else {
612+ contextContent = fmt .Sprintf ("<Context docBase=\" ${user.home}/app\" reloadable=\" false\" >\n </Context>\n " )
613+ t .context .Log .Info ("Created %s with docBase pointing to application directory" , contextXMLName )
603614 }
604615
605- xmlStr := string (xmlBytes )
606- xmlStr = strings .TrimSpace (xmlStr )
616+ if err := os .WriteFile (contextXMLPath , []byte (contextContent ), 0644 ); err != nil {
617+ return fmt .Errorf ("failed to write %s: %w" , contextXMLName , err )
618+ }
607619
608- contextContent = injectDocBase (xmlStr , "${user.home}/app" )
609- t .context .Log .Info ("Merged META-INF/context.xml with %s - realm and resource configurations preserved" , contextXMLName )
620+ if contextXMLName != "ROOT.xml" {
621+ rootXMLPath := filepath .Join (t .tomcatDir (), "conf" , "Catalina" , "localhost" , "ROOT.xml" )
622+ if err := os .Remove (rootXMLPath ); err != nil && ! os .IsNotExist (err ) {
623+ return fmt .Errorf ("failed to remove ROOT.xml: %w" , err )
624+ }
625+ }
610626 } else {
611- contextContent = fmt .Sprintf ("<Context docBase=\" ${user.home}/app\" reloadable=\" false\" >\n </Context>\n " )
612- t .context .Log .Info ("Created %s with docBase pointing to application directory" , contextXMLName )
613- }
614-
615- if err := os .WriteFile (contextXMLPath , []byte (contextContent ), 0644 ); err != nil {
616- return fmt .Errorf ("failed to write %s: %w" , contextXMLName , err )
627+ t .context .Log .Info ("Context XML %s already exists (e.g. from external config), skipping generation" , contextXMLName )
617628 }
629+ } else {
630+ warMatches , err := filepath .Glob (filepath .Join (buildDir , "*.war" ))
631+ if err == nil && len (warMatches ) == 1 {
632+ warFilename := filepath .Base (warMatches [0 ])
633+ contextContent := fmt .Sprintf ("<Context docBase=\" ${user.home}/app/%s\" reloadable=\" false\" >\n </Context>\n " , warFilename )
634+
635+ contextXMLDir := filepath .Dir (contextXMLPath )
636+ if err := os .MkdirAll (contextXMLDir , 0755 ); err != nil {
637+ return fmt .Errorf ("failed to create context directory: %w" , err )
638+ }
618639
619- if contextXMLName != "ROOT.xml" {
620- rootXMLPath := filepath .Join (t .tomcatDir (), "conf" , "Catalina" , "localhost" , "ROOT.xml" )
621- if err := os .Remove (rootXMLPath ); err != nil && ! os .IsNotExist (err ) {
622- return fmt .Errorf ("failed to remove ROOT.xml: %w" , err )
640+ if _ , statErr := os .Stat (contextXMLPath ); os .IsNotExist (statErr ) {
641+ if err := os .WriteFile (contextXMLPath , []byte (contextContent ), 0644 ); err != nil {
642+ return fmt .Errorf ("failed to write %s: %w" , contextXMLName , err )
643+ }
644+
645+ if contextXMLName != "ROOT.xml" {
646+ rootXMLPath := filepath .Join (t .tomcatDir (), "conf" , "Catalina" , "localhost" , "ROOT.xml" )
647+ if err := os .Remove (rootXMLPath ); err != nil && ! os .IsNotExist (err ) {
648+ return fmt .Errorf ("failed to remove ROOT.xml: %w" , err )
649+ }
650+ }
651+ t .context .Log .Info ("Created %s with docBase pointing to %s" , contextXMLName , warFilename )
652+ } else {
653+ t .context .Log .Info ("Context XML %s already exists (e.g. from external config), skipping generation" , contextXMLName )
623654 }
655+ } else if len (warMatches ) > 1 {
656+ t .context .Log .Warning ("Multiple WAR files found in build directory; context_path not applied" )
624657 }
625658 }
626659
0 commit comments